vendredi 22 septembre 2017

C: How to discern between a string and integer? Output is printing ints fine, but not for strings

hope you're all having a good day! Well, I would like to explain what my program does first. My program tokenizes a string of input from the user, and from those tokens, discern whether or not they are a string or int(if any alphabetical character is attached to a number, meaning it is adjacent to any number - it is then treated as a string).

Hence, the following input: "this is 1string 123 1", should output STR STR STR INT INT. What I did(or attempted to do, rather) to solve this problem was: take the input string, proceed to tokenize/split it(with a space delimiter), and then iterate through the split token(using a for loop) to see whether or not the split token, has any character from the alphabet(upper-case or lower-case).

Now, that last sentence is the problem. What I did was import the stdbool.h header, and make a boolean called foundChar. If any alphabetical characters were found, then I would set foundChar to true. It makes sense to me. From there, I would do a simple if check to see if foundChar == true. However, now when I input a string, it prints blanks? I'm not sure if the way I'm using booleans is correct in C, or where to exactly pinpoint what is going wrong here. If you could please take a look at my code, and provide some sort of feedback, I would appreciate it very much! Thank you again for taking the time to read this.

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

// main
int main(void){


// declare our stuff for stuffs
char string[255];
char *tokenizedString;
const char delimiter[2] = " ";
char *pch;
bool found = false;
int truth = 0;

// prompt our user
do{

    do{
        printf(">");
        fgets(string, 65, stdin);

    } while( strlen(string) <= 0 );

    // remove it "\n" so it can terminate from newline input
    pch = strstr(string, "\n");
    strncpy(pch,"\0",1);

    // tokenize the input string
    tokenizedString = strtok(string, delimiter);

    while(tokenizedString != NULL){

        // check for any character in the token
        // if found = true, then it's a string by our premade 
definition
        for(int i = 0; i < strlen(tokenizedString); i++) {
            if( (i >= 'a' && i <= 'z') 
              || (i >= 'A' && i <= 'Z') ) {
                found = true;                
            }
        }

        // if we find a char in the string           
        if(found == true) { 
            printf("STR ");
        }


        // if the token is a number
        else if(isdigit(*tokenizedString))
            printf("INT ");


        tokenizedString = strtok(NULL, delimiter);


        // reset charFound to zero for our next token
        found = false;

    }

    printf("\n");    

} while(!(strlen(string) == 0));
}// end of program

OUTPUT

why are

_________(nothing actually prints here, blank! STR STR should print, same for the next string below!)

you not printing output for string

_________(nothing prints here as well, should print STR STR STR STR STR STR)

123

INT

1st

INT

nor printing the right stuff

>

DESIRED OUTPUT

why are

STR STR

you not printing output for string

STR STR STR STR STR STR

123

INT

1st

STR

nor printing the right stuff

STR STR STR STR STR

>

Aucun commentaire:

Enregistrer un commentaire