gpt4 book ai didi

C源代码,将字符串中单词的首字母从小写更改为大写

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:06 24 4
gpt4 key购买 nike

我有一个 C 源代码,但我有一个问题。我想将我输入的字符串中单词的第一个字母从小写转换为大写,但它会将所有字母更改为大写。你能帮我解决这个问题吗?

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char sentence[100];
int count, ch, i;
int str[32];
printf("Enter a sentence \n");
for (i = 0; (sentence[i] = getchar()) != '\n'; i++)
{
;
}
sentence[i] = '\0';
/* shows the number of chars accepted in a sentence */
count = i;
printf("The given sentence is : %s", sentence);
printf("\n Case changed sentence is: ");
for (i = 0; i < count; i++)
{
ch = islower(sentence[i])? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}
getch();
}

例如

输入:欢迎来到谢里夫大学

期望的输出:欢迎来到谢里夫大学

实际输出:WELCOME TO SHARIF UNIVERSITY

最佳答案

您必须检查当前字符是否为空格,然后仅对空格后的字符使用 toupper

关于C源代码,将字符串中单词的首字母从小写更改为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27277156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com