I need help with the following:
Separate every consecutive array of uppercase, lowercase letters and digits into separate strings from the input string. Assume that the input string only contains uppercase, lowercase letters and digits. Input string doesn't have blank spaces.
Example:
Input string: thisIS1inputSTRING
OUTPUT:
1. string: this
2. string: IS
3. string: 1
4. string: input
5. string: STRING
The following program doesn't give any output:
#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;
}
if (isdigit(str[i]) || isupper(str[i]) || islower(str[i]))will always be true. So, you set the first character ofwordto\0many times and print it many times.int flag;. Set it to 1, when the current character is lowercase, 2 when the current character is uppercase, 3 when the current character is a digit. Then, you need to make some changes to your code.word[]buffer either. It may be more apparent what has to be done if you think about how this is doable without it.gets(), which has unavoidable risk of buffer overrun.