简体   繁体   中英

Alternate letters in upper case

As the below code should convert the given String into alternative upper or lower case.A string S (only alphabets) is passed as input. The printed output should contain alphabets in odd positions in each word in uppercase and alphabets in even positions in each word in lowercase.

#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;
}

The input will be tREE GiVES us fruiTS and the expected output should be TrEe GiVeS Us FrUiTs BUT what im getting is just first string TrEe What should i do to get full string

scanf("%s, str) reads in a string until the first whitespace character. So when you type "tree gives us fruits" it reads in "tree" and then see's the whitespace and stops.

Try using fgets(str, 100, stdin) instead

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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