Back to: C Tutorials For Beginners and Professionals
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.
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:
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:
So, in this function, we haven’t done a lot of conditions. So, we have seen a validation function.
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.