van1234
van1234

Reputation: 11

How to check a string contain a certain value in C

#include <stdio.h>
#include <string.h>   
#define CHAR_SIZE 35

//Function to remove white space
char *remove_white_spaces(char *str)
{
    int i = 0, j = 0;
    while (str[i])
    {
        if (str[i] != ' ')
            str[j++] = str[i];
        i++;
    }
    str[j] = '\0';
    return str;
}

void main()
{
    int i = 0; 
    char str[CHAR_SIZE];
    
    printf("\nKey in input: ");
    fgetchar();
    fgets(str , CHAR_SIZE, stdin);
    
    //Remove white space 
    remove_white_spaces(str);
    printf("%s",str);

    //for (i = 0; str[i] != '\0'; ++i);
    //printf("Length of the string: %d", i);

    if (str[i] == '0' || str[i] == '1' )
    {
        printf("CORRECT");
    }
    else
    {
        printf("Wrong Input");
    }

}

I want to check whether the user has type in the correct input. For example, I have key in 0 01111110 10100000000000000000000. After removing the white space, the str input became 00111111010100000000000000000000. From this str, I want to check that the user has only key in 0 and 1. The output of the result I got was correct which is shown below1. Output of result

However, when the user key in another value including 0 and 1. The output I suppose to get is the wrong input. But I obtained Correct as the result which is shown below2.

Output of result

Additional question, How do I implement an if statement that the str has to only have 32 characters to continue otherwise it has to break and the user key has to key in 32 characters only. Can I do it in a while loop instead of an if statement so that the user would not need to run the code again?

Upvotes: 1

Views: 383

Answers (3)

ossama elbelamachi
ossama elbelamachi

Reputation: 16

First of all, you are checking that str[i] should be equal to 0 and equal to 1 – and that doesn't make any sense, because an element in the array can be only one value, 0 or 1; so, you should test if (str[i] == '0' || str[i] == '1'). And, before that, you should initialize i: int i = 0.

Edit you must loop over elements of the string

int check = 0;
while (str[i] != '\0')
{
    if (str[i] == '0' || str[i] == '1')
         i++;
    else {
        check = 1;
        break;
    }
    
}
if (check == 0){
     print("CORRECT");
}
else {
    printf("WRONG INPUT");
}

Upvotes: 0

Clinton Kavai
Clinton Kavai

Reputation: 152

You could use strtok to extract your characters. Also there's a flaw in your logic. it should be if (str[i] == '0' || str[i] == '1' to check if the value is '0' OR '1'. Here's a sample implementation you could refer to:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHAR_SIZE 100

int main()
{
    char str[CHAR_SIZE];
    printf("\n Key in value: ");
    getchar();
    fgets(str, CHAR_SIZE, stdin);
    char *tok;
    tok = strtok(str, "\n");
    int i = 0;
    tok++; //skip the first character which is a space
    while (*tok != 0x00)
    {

        if (*tok <= 0x31 && *tok >= 0x30)
            tok++;

        else
        {
            printf("Wrong number input ==> %c \n", *tok);
            break;
        }
    }
}

Upvotes: 1

initialize i: putting the equivalent of C's

int i = 0; 

in your prog lang before entering the while loop should do the job.

Upvotes: 0

Related Questions