【发布时间】:2015-10-08 15:17:48
【问题描述】:
所以我想创建一个代码,将字符串数组中每个单词的第一个字母大写,然后以相反的顺序输出字符串。我无法反向打印数组,但除此之外,这就是我想出的:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char string[100];
int i, j;
char newString[100];
printf("\nEnter string: ");
gets(string);
for (i=0; i <strlen(string); i++){
if (string[i] == ' ' && isalnum(string[i+1])==1){ //if the character is preceded by a space
newString[i] = toupper(string[i+1]); //copy the uppercase of the character to newString
}
if (isalpha(string[0]) == 1){ //capitalize the first character in the string if it is a letter
newString[0] = toupper(string[0]); //copy character to newString
}else{
newString[i] = string[i];
}
}
printf("%s", newString); //preferably, the newString should be printed in reverse order, but I can't seem to do it.
}
如果:
输入:curran lennart
此代码的假定输出:Curran Lennart
(我想要的:narruC tranneL)
事实上,我得到的只是以下输出:
curran lennarta
“kate daniels”的输入返回“kate daniels”。如果输入是:
julie olsen
输出是:
julie olsenw
请帮忙。 :(
【问题讨论】:
-
man isalpha给出RETURN VALUE : The values returned are nonzero if the character c falls into the tested class, and zero if not.。因此,即使isalnum(string[i+1])==1是字母数字字符,您也无法确定true。