我需要用以下帮助创建字符串数组:从输入字符串
独立大写的每个连续的阵列,小写字母和数字到从输入字符串单独字符串。假设输入字符串只包含大写,小写字母和数字。输入字符串没有空格。
Example:
Input string: thisIS1inputSTRING
OUTPUT:
1. string: this
2. string: IS
3. string: 1
4. string: input
5. string: STRING
下面的程序不给任何输出:
#include <stdio.h>
#include <string.h>
#include<ctype.h>
int main() {
char str[512], word[256];
int i = 0, j = 0;
printf("Enter your input string:");
gets(str);
while (str[i] != '\0') {
//how to separate strings (words) when changed from
//uppercase, lowercase or digit?
if (isdigit(str[i]) || isupper(str[i]) || islower(str[i])) {
word[j] = '\0';
printf("%s\n", word);
j = 0;
} else {
word[j++] = str[i];
}
i++;
}
word[j] = '\0';
printf("%s\n", word);
return 0;
}
'如果(ISDIGIT测试( str [i])|| isupper(str [i])|| islower(str [i]))将始终为真。因此,您将'word'的第一个字符设置为'\ 0'多次并打印多次。 –
@Cool Guy设置(str [i] =='')if语句会将输入字符串中的单词与输入字符串中的空格分开。是否有可能只修改上述程序中的if语句,以便在每次大写,小写或数字更改之后进行分离? – user300048
有必要检查变化的字符类型。 – BLUEPIXY