Menu Close

C语言练习题:把字符串从小写字母转换为大写

在下面的程序中,用户将被要求输入一个小写字符串,程序会将其转换为大写字符串。 程序中遵循的逻辑:所有小写字符(a 到 z)的 ASCII 值范围为 97 到 122,其对应的大写字符(A 到 Z)的 ASCII 值比它们小 32。 例如,“a”的 ASCII 值是 97,“A”的 ASCII 值是 65 (97-32)。 这同样适用于其他字母。

基于这个逻辑,我们编写了下面的 C 程序进行转换。


#include<stdio.h>
#include<string.h>
int main(){
   char str[25];
   int i;

   printf("Enter the string:");
   scanf("%s",str);

   for(i=0;i<=strlen(str);i++){
      if(str[i]>=97&&str[i]<=122)
         str[i]=str[i]-32;
   }
   printf("\nUpper Case String is: %s",str);
   return 0;
}

结果:

Enter the string:abcdefghijklmN

Upper Case String is: ABCDEFGHIJKLMN

 

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Leave the field below empty!

Posted in C 决策和循环语句, C语言习题集

Related Posts