用c++编程
写程序,以键盘输入一行字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test.txt”中保存。要求:1,使用字符型数组。2,在主函数中输入字符串。3,在子函数中实现小写转换成大写。4,将结果保存到磁盘文件test.txt (帮帮忙!)
写程序,以键盘输入一行字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test.txt”中保存。要求:1,使用字符型数组。2,在主函数中输入字符串。3,在子函数中实现小写转换成大写。4,将结果保存到磁盘文件test.txt (帮帮忙!)
答案:
#include<iostream>
#include <fstream>
using namespace std;
void fun(char *s){
int i=0;
while(s[i] != '\0'){
if(s[i]>='a' && s[i] <='z')
s[i]-=32;
i++;
}
}
int main (){
char s[255];
cin>>s;
fun(s);
string filename = "test.txt";
ofstream outfile;
outfile.open(filename.c_str(), ofstream::out);
if(!outfile || !outfile.is_open()){
cerr << "error:unable to open output file:" <<filename << endl;
return 0;
}
outfile << s << endl;
outfile.flush();
outfile.close();
return 0;
}#include <iostream.h>
#include <fstream.h>
#include <string.h>void ConvertLower(char* str)
{
int nLen = strlen(str);
for(int i=0; i<nLen; i++)
{
if(str[i] < 'a')
continue;
if(str[i] > 'z')
continue;
str[i] -= 32;
}
}
void main()
{
char strText[260];
gets(strText);
ConvertLower(strText);
streambuf* coutBuf = cout.rdbuf();
cout << strText << endl;
ofstream of("test.txt");
streambuf* fileBuf = of.rdbuf();
cout.rdbuf(fileBuf);of.flush();
of.close();
cout.rdbuf(coutBuf);}
这样:
#include<iostream>
#include <fstream>
using namespace std;
void fun(char *s){
int i=0;
while(s[i] != '\0'){
if(s[i]>='a' && s[i] <='z')
s[i]-=32;
i++;
}
}
int main (){
char s[255];
cin>>s;
fun(s);
string filename = "test.txt";
ofstream outfile;
outfile.open(filename.c_str(), ofstream::out);
if(!outfile || !outfile.is_open()){
cerr << "error:unable to open output file:" <<filename << endl;
return 0;
}
outfile << s << endl;
outfile.flush();
outfile.close();
return 0;
}
谢谢采纳
上一个:求c++编程..........
下一个:C++编程编程