Say I have a string named speed , and I want to check it against acceptable string values . How would I do this , I know how to compare integers but not strings .
How to compare strings - c?
I assume that you have two strings to compare. In C language, string are character array. So, Let the string to compare is speed, with a set of given strings. let you have defined that set of strings as an array of string , i.e., two dimensional array of charecter. With these assumptions, perform following steps:
char *spped;// or you can declare as char spped[SIZE];
char *Values[MAX];// MAX is maximum number of allowable string values.
int i;
for(i=0;i%26lt;MAX;i++)
{
if(strcmp(speed, Values[i]) == 0)
{printf("string is valid");}
}
you should include %26lt;string.h%26gt; to use strcmp().
If U like the answer, PLZ, do rate as best answer
Reply:use the strcmp() or strncmp() funcitons.
Or, you could write your own function to compare byte for byte.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment