0

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;
}
7
  • 2
    if (isdigit(str[i]) || isupper(str[i]) || islower(str[i])) will always be true. So, you set the first character of word to \0 many times and print it many times. Commented Mar 2, 2016 at 12:12
  • 1
    There is a need to examine the character type of changes. Commented Mar 2, 2016 at 12:21
  • 3
    You need to keep track of changes. Use a variable like 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. Commented Mar 2, 2016 at 12:26
  • 2
    While changing your code, consider there doesn't appear to be a need for a word[] buffer either. It may be more apparent what has to be done if you think about how this is doable without it. Commented Mar 2, 2016 at 12:31
  • 3
    Note: You shouldn't use gets(), which has unavoidable risk of buffer overrun. Commented Mar 2, 2016 at 12:31

1 Answer 1

2

Your solution went wrong as also written in the comments the statement (isdigit(str[i]) || isupper(str[i]) || islower(str[i])) is always true.

If you want to stick to your solution using an if statement then you have to check the next following character. If the next charactertype differs from the actual character type, then you have to print out your word, because the next character is a different type.

I adjusted your code to the following:

#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 

            // Is the next character the end of the string?
            if(str[i+1] != '\0'){   // <<<
                //uppercase, lowercase or digit?
                if (
                    isdigit(str[i]) && (isupper(str[i+1]) || islower(str[i+1])) // <<<
                    || isupper(str[i]) && (isdigit(str[i+1]) || islower(str[i+1]))  // <<<
                    || islower(str[i]) && (isupper(str[i+1]) || isdigit(str[i+1]))  // <<<
                ){
                        word[j] = str[i];   // <<<
                        word[j+1] = '\0';   // <<<
                        printf("%s\n", word);
                        j = 0;
                } else {
                        word[j++] = str[i];
                }   
            }
            else {
                // End of the string, write last character in word
                word[j] = str[i];   // <<<
                word[j+1] = '\0';   // <<<
                printf("%s\n", word);
            }
            i++;
    }
    return 0;
}

This would lead to the following output:

Enter your input string:this
IS
1
input
STRING

You can test it by your own link[^]

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't work correctly, last character is missing in the output.
Oh I see, thanks for your advice, I had to add a line in the end. I know this is not nice coded but is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.