简体   繁体   中英

Create array of strings from an input string

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

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[^]

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