为了更好地理解,我们始终建议您学习下面列出的C语言编程基础主题
在这个程序中,我们需要将大写字母转换为小写,将小写字母转换为大写。该程序的逻辑是,首先包含头文件 string.h 来处理字符串操作。从用户读取一个句子并将其保存到变量 sentence 中。使用 for 循环遍历它以获取句子的长度。然后将句子的长度存储在变量 count 中。然后显示字符串。然后使用条件为 i
islower() 函数用于检查字符是否为小写。如果字符是小写,则它将返回一个大于零的数字。否则,它将返回零。islower() 函数的原型如下。
int islower(int ch);
返回类型是 整数类型。参数也是一个整数,这意味着我们传入的字符的 ASCII 值。
步骤 1:包含头文件以在 C 程序中使用内置头文件。
步骤 2:将变量 count, ch, i 声明为整数,将 sentence 声明为字符。
步骤 3:从用户读取一个句子到变量 sentence 中。
步骤 4:使用条件为 sentence[i]=getchar()!=null 的 for 循环,将 i 增加 1。
步骤 5:设置 count = i。
步骤 6:将输入的句子显示为句子。
步骤 7:使用 printf 函数显示结果句子。
步骤 8:使用条件为 i 的 for 循环
步骤 9:检查 sentence[i] 是否为小写。如果是小写,则转换为大写。
步骤 10:使用 putchar() 函数显示 sentence[i]。
#include <stdio.h>
#include <ctype.h>
void main()
{
char sentence[100];
int count, ch, i;
printf("Enter a sentence\n");
for (i = 0;
(sentence[i] = getchar()) != '\n'; i++)
{
;
}
count = i;
/*convert the lower case letters to upper case and vice-versa */
printf("\nThe input sentence is %s ", sentence);
printf("\nA resultant sentence is : ");
for (i = 0; i < count; i++)
{
ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}
} /*End of main() */
Enter a sentence good morning The input sentence is: good morning A resultant sentence is: GOOD MORNING