在下面的 C 程序中,将要求用户输入一个字符串(可以是完整的大写或部分大写),然后程序会将其转换为完整的(所有字符均为小写)小写字符串。
我们在以下程序中使用的逻辑是:所有大写字符(A-Z)的 ASCII 值范围为 65 到 90,其对应的小写字符(a-z)的 ASCII 值都比它们大 32。
例如,“A”的 ASCII 值是 65,“a”的 ASCII 值是 97 (65+32)。 这同样适用于其他字符。
/* C program to convert uppercase string to
* lower case
* written by: Chaitanya
*/
#include<stdio.h>
#include<string.h>
int main(){
/* This array can hold a string of upto 25
* chars, if you are going to enter larger string
* then increase the array size accordingly
*/
char str[25];
int i;
printf("Enter the string: ");
scanf("%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nLower Case String is: %s",str);
return 0;
}
结果:
|
1 2 3 |
Enter the string: ABCDEFG Lower Case String is: abcdefg |
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!