繁体   English   中英

大写替代字母

[英]Alternate letters in upper case

由于下面的代码应该将给定的字符串转换为可选的大写或小写。字符串 S(仅限字母)作为输入传递。 打印的 output 应在每个单词的奇数位置包含大写字母,在每个单词的偶数位置包含小写字母。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
scanf("%s",str);
for(i=0;str[i]!='\0';i++){
if((i%2)==1)
str[i]=tolower(str[i]);
else
str[i]=toupper(str[i]);
}
printf("%s",str);
return 0;
}

输入将是tREE GiVES us fruiTS和预期的 output 应该是TrEe GiVeS Us FrUiTs但我得到的只是第一个字符串我应该怎么做才能得到完整的字符串

scanf("%s, str)读取一个字符串,直到第一个空白字符。因此,当您键入“tree give us fruits”时,它会读取“tree”,然后看到空白并停止。

尝试改用fgets(str, 100, stdin)

https://www.cplusplus.com/reference/cstdio/fgets/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM