How to Validate a String in C

How to Validate a String in C Language with Examples

In this article, I am going to discuss How to Validate a String in C Language with Examples. Please read our previous article where we discussed How to Count Vowels and Consonants in a String in C Language with Examples.

How to Validate a String in C Language?

In this article, we will see how to validate a string. We have to check whether a given string is valid or not. Most of the time while creating a login and an account, we have to mention a user name and password. And for most passwords, we find that a valid password is required.

So, what is a valid password? Only alphabets or numbers are allowed special characters are not allowed. A similar thing we have to check in a string.

How to Validate a String in C Language with Examples

We have a string here, that contains alphabets and numbers. It is a valid string if any special character is there then it is invalid. So, we have to check whether the string is valid or not.

There are two methods. One is a simple method that we are going to show you. The second method is using regular expressions. If you want to learn about regular expression then you can learn and use it in C / C++ programs. Now let us follow the basic method.

In this method, we have to scan for this entire string and find whether each and every alphabet is valid or not if any one of the alphabets is not valid then we should say it is invalid. So let us write our separate functions. Let us modify our valid string to invalid by adding ‘@’ in the above string:

How to Validate a String in C Language with Examples

So, for performing this procedure we have to check for each and every alphabet. And the function ‘ValidateString’ will take a string as a parameter. It should return whether it is a true or false means valid or not. It is valid if it returns 1 otherwise it is invalid if it returns ‘0’.

Program to Validate a String in C Language:
#include <stdio.h>
#include <stdlib.h>
int ValidateString (char *B)
{
        int i;
        for (i = 0; B[i] != ‘\0’; i++)
        {
               if (!(B[i] >= 65 && B[i] <= 90) && !(B[i] >= 97 && B[i] <= 122) && !(B[i] >= 48 && B[i] <= 57))
              {
                    return 0;
              }
       }
       return 1;
}
int main ()
{
        char *B = “Rahul@7928”;
        int j;
        printf (“String is \”%s\”\n”, B);
        if (ValidateString (B))
        {
              printf (“Valid String”);
        }
        else
        {
             printf (“Invalid String”);
        }
        return 0;
}

So, in this function, we haven’t done a lot of conditions. So, we have seen a validation function.

Output:

Validate a String Code in C Language

In the next article, I am going to discuss How to Reverse a String in C Language with Examples. Here, in this article, I try to explain How to Validate a String in C Language with Examples. I hope you enjoy this How to Validate a String in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.