C语言编程,从键盘输入一字符串,将其中的小写字母全部转换成大写字母

然后输出到一个磁盘文件“file.txt”中保存,输入的字符串以“#”结束

代码如下:

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

int main() {         

FILE *fp;

if((fp=fopen("123.txt","a"))==NULL)

printf("file cannot open \n");

else  

printf("file opened for writing \n");

char ch;

while((ch = getchar()) != '\n'){

fputc(toupper(ch),fp);                                                                                           

}     

fputc('#',fp);

if(fclose(fp)!=0)

printf("file cannot be closed \n");

else  

printf("file is now closed \n");

return 0;

}    

程序运行如下:

扩展资料

C语言对文件输入可以用fputc函数,只需要循环遍历输入,把输入的每个碧碰字符串进行大写毁慧培的转换,大小写转换可以通过C语纤唯言内置于ctype.h的toupper函数来转换,没转换一个字符串可以直接写入到文件中,最后在追加一个#好字符,就完成功能了。

参考资料:百度百科-C语言


小写字母全部转换成大写字母的源代码如下:

#includevoid main() 

file *fp; 

char str[100]; int i=0; 

if((fp=fopen("test.txt","wt"))==null) 

printf("can't open this file.\n");

exit(0);

 } 

printf("input a string:\n"); 

gets(str); 

while (str[i]) 

if(str[i]>='a'&&str[i]<='z') 

str[i]=str[i]-32; 

fputc(str[i],fp); 

i++; 

fclose(fp); 

fp=fopen("test.txt","rt"); 

fgets(str,100,fp); 

printf("%s\n",str); 

fclose(fp); 

}

扩展资料

1、编程语言终究开始引入了函数的概念,即允许程序对型燃闹代码进行段拦断行。如果已经完成,不再使用goto语句来表示卜罩代码的断行。函数调用后,函数将回到下一条指令。

2、如果goto语句已经过时,那么对程序创建无限循环应该如何去做呢,这是一些硬件工程师可能会疑惑的问题。毕竟,之前都是通过创建一个goto语句然后再返回到main语句。


#include<卜拿stdio.h>型旁搭

#include<stdlib.h>

void main()

{

FILE*fp;

char ch;

int i=0;

printf("输入字符以#结尾:");

fp=fopen("file.txt","w");

ch=getchar();

 

while(ch!='#')

{

fputc(ch-32,fp);

ch=getchar();

}

fclose(fp);

fp=fopen("file.txt","r");

ch=fgetc(fp);

while(ch!=EOF)

{

putchar(ch);

ch=fgetc(fp);

}

printf("\n"启神);

fclose(fp);


}


#include<stdio.h>

int main()
{ int i=1;
char m;
printf("请输入一顷州串字符(以#结尾):");
while(1)
{m=getchar();
if(m=='#'裤乎前||i>10)
break;
else if(m>胡清=97&&m<=122)
printf("%c",m-32);
else
printf("%c",m);
i++;}
return 0;
}