top

Search

C Tutorial

.

UpGrad

C Tutorial

String Comparison in C

Strings are used to store text or characters. In C programming string is a sequence of characters ending with a null character \0. This null character indicates the termination of the string. Strings are a type of one-dimensional character array or a collection of character values.  The basic syntax to declare a string is defined as

char string_name[size];

Here string_name is the name of the string variable and size defines the length of the string.

Example: char name[10] = “BEAUTIFUL”;

Or 

char name[ ] = “BEAUTIFUL”;

In this example, a string is declared with the identifier “name”.  The string is initialized to “BEAUTIFUL”. The compiler will automatically put a null character after the last character of the string. If the size is not mentioned then the compiler will calculate the size by itself. By adding one more value to the number of characters length of the string can be calculated.

Example: C program to represent string initialization.

#include <stdio.h>
Void main()
{
char name[5] = “GOOD”;
printf( “%s \n”, name);
return 0;
}

Output:

After execution, the output will be
GOOD

String comparison in C is used to check the plagiarism of documents, compare the passwords while signing in, etc. In the next section, a full description is given related to String Comparison in C, string comparison in c without using strcmp, string comparison in C without using library function, examples of string comparison, etc.

What is String Comparison in C?

To detect if two strings are identical or not string comparison is used. This is executed by utilizing some built-in function or by comparing every character of both strings. 

To compare the strings in the C programming language 4 basic methods are used.

  • String comparison by using strcmp() String Library function.

  • String comparison without using strcmp() function .

  • String comparison by using pointers.

  • String comparison by using recursion.

Methods of string comparison in C

There are various methods of string comparison in C as mentioned below: 

  • String comparison by using strcmp() String Library function

The string.h header file consists of some predefined string library functions. The string library functions are used for operations on strings. strcmp()  is a predefined library function that is used to compare two strings. Two strings are given to the strcmp() function as input arguments. The function then compares both strings lexicographically. It returns an integer value of zero, a positive value, and a negative value as a result. The syntax used to define strcmp() function is presented as

int strcmp (const char* stng1, const char* stng2);

Here two inputs string1 and string2 are passed as strings. The int signifies that the output of strcmp function is an integer value. The result after the comparison of both strings depends on 3 different cases which are listed below.

1.  The return value is 0 when both strings are identical.

2. Returns a positive value if a character's ASCII value in the first string is bigger than a character's ASCII value in the second string.

3. Return a negative value if the ASCII value of a character in the first string is smaller than the ASCII value of a character in the second string.

Example:

#include <stdio.h>
#include <string.h>
int main()
{
char stng1[20];
char stng2[20];
int  var;
printf(“Enter first string:”);
scanf(“%s”, stng1);
Printf(“Enter second string:”);
scanf(“%s”, stng2);
var = strcmp(stng1, stng2);
if(var ==0)
printf (“strings are equal”);
else
printf (“strings are not equal”);
return 0;
}

Output:

Enter first string: morning
Enter second string: night
strings are not equal

Algorithm:

1. Two character arrays stng1 and stng2 are declared. 

2. printf and scanf functions are used to take input from the user.

3. strcmp() function is used to compare the above two strings stng1 and stng2.

4. After execution if the function returns 0 then both strings are equal else strings are not equal.

Keynotes: 

1. Here stdio.h means single input single output. This is a built-in header file in the C programming language. It contains information related to input and output functions.

2. Printf is an output function that indicates “print formatted”.

3. Scanf is an input function that is used to read input from the user.

4. Int main() function is used as the starting point of program execution. It directs the calls to other functions in the program and thus controls program execution.

  • String comparison without using strcmp() function 

String comparison without using strcmp() function is possible. String comparison can be done by using a loop.  In this process, two strings are compared by traversing each index using a loop and equating every character one after another.

Example: 

#include <stdio.h>
int StringComparison(char[], char[]);
int main()
{
char stng1[20];
char stng2[20];
int  var;
printf(“Enter first string:”);
scanf(“%s”, stng1);
Printf(“Enter second string:”);
scanf(“%s”, stng2);
var = StringComparison (stng1, stng2);
if(var ==0)
printf (“strings are equal”);
else
printf (“strings are not equal”);
return 0;
}
int StringComparison(char a[], char b[])
{
int flag=0,i=0;
while (a[i]!=’\0’ &&b[i]!=’\0’)
{
If(a[i]!= b[i])
{
flag = 1;
break;
}
i ;
}
if (a[i]!=’\0’ ||b[i]!=’\0’)
return1;
if(flag==0)
return 0
else
return1;
}

Output:

Enter first string: good
Enter second string: excellent
strings are not equal

Algorithm:

1. Two character arrays stng1 and stng2 are declared. 

2. A function named StringComparison() is built. 2 strings are given as arguments to the function. 

3. Then a while loop is formed to compare the characters one by one. The strings are identical if the function returns 0 otherwise the strings are not identical.

  • String comparison by using pointers

Example:

#include <stdio.h>
int StringComparison(char *, char *);
int main()
{
char stng1[20];
char stng2[20];
int  var;
printf(“Enter first string:”);
scanf(“%s”, stng1);
Printf(“Enter second string:”);
scanf(“%s”, stng2);
var = StringComparison (stng1, stng2);
if(var ==0)
printf (“strings are equal”);
else
printf (“strings are not equal”);
return 0;
}
int StringComparison(char *a, char *b)
{
int flag = 0;
while (*a != ‘\0’ && *b != ‘\0’)
{
if (*a != *b)
{ 
flag = 1;
}
a ;
b ;
}
if(*a != ‘\0’ || *b != ‘\0’)
return 1;
if (flag ==0)
return 0;
else
return 1;
}

Output:

Enter first string: construct
Enter second string: develop
strings are not equal

Algorithm:

1. Two character arrays stng1 and stng2 are declared. 

2. A function named StringComparison() is built. 2 strings are given as arguments to the function. 

3. The address of ” stng1” is stored in ‘a’ pointer and the address of “stng2” is stored in ‘b’ pointer.

4. Inside the function a while loop is formed which runs until the pointer a or b does not reach a null character.

  • String comparison by using recursion

The recursion function is also used to compare two strings. Here lengths of both strings. Then a function is built which keeps calling itself until it meets the desired criteria.

Example: 

#include<stdio.h>
#include<string.h>
Int StringComparison(char stng1[ ], char stng2[ ])
{
static int i  = 0;
int x = strlen(stng1);
int y = strlen(stng2);
static int c = 0;
if (x != y)
{
return 0;
}
else if (i < x)
{
if (stng1[i] == stng2[i])
c ;
i ;
StringComparison(stng1, stng2);
}
if (c ==i)
{
return 1;
}
return 0;
}
int main()
{
char stng1[20], stng2[20], c;
printf(“Enter first string”);
fgets(stng1, 20, stdin);
printf(“Enter second string”);
fgets(stng2, 20, stdin);
c = StringComparison(stng1, stng2);
if (c)
{
printf(“Strings are equal”);
else
{
printf(“strings are not equal”);
}
return 0;
}
}
}

Output:

Enter first string construct
Enter second string construct
Strings are equal

Algorithm: 

1. Two character arrays stng1 and stng2 are declared. 

2. A function named StringComparison() is built. The length of both strings is computed using stlen() function. If lengths are equal StringComparison() function will return 0.

3. If the lengths are equal then the characters are compared. The first character of stng1 i.e. stng1[i] is compared with the first character of stng2 i.e. stng2[i]. If both characters are the same the value of the count is increased by 1.

4. The function will keep calling itself until it meets the required criteria i.e. i < x is false.

5. If c = i it signifies the strings are equal and the StringComparison() function will return 0.

6. If the strings are not equal the StringComparison() function will return 1.

Conclusion

A brief idea about the string function is given in the beginning. Then various methods are discussed to compare two strings. The significance of strcmp() function is explained. A comparison of two strings without using a library function is represented. Also Comparison of two strings using pointers and recursion are depicted.

FAQs

1. What are strings?

Strings are used to store text or characters. In C programming string is a sequence of characters ending with a null character \0. Strings are a type of one-dimensional character array or a collection of character values.  

2. What is the significance of a null character?

This null character indicates the termination of the string.

3. What is the syntax to define a string?

The basic syntax to declare a string is defined as

4. What is the char string_name[size];?

Here string_name is the name of the string variable and size defines the length of the string

5. What is string comparison in C?

To detect if two strings are identical or not string comparison is used. This is executed by utilizing some built-in function or by comparing every character of both strings. 

6. What is strcmp()?

Strcmp()  is a predefined library function that is used to compare two strings.

7. What is the syntax to define strcmp()?

The syntax used to define strcmp() function is presented as

int strcmp (const char* stng1, const char* stng2);

Here two inputs string1 and string2 are passed as strings. The int signifies that the output of strcmp function is an integer value.

8. What is stdio.h?

Here stdio.h means single input single output. This is a built-in header file in the C programming language. It contains the information related to input and output functions.

9. What is string.h?

The string.h header file consists of some predefined string library functions. The string library functions are used for operations on strings. 

10. What is int main ()?

Int main() function is used as the starting point of program execution. It directs the calls to other functions in the program and thus controls program execution

11. What is printf?

Printf is an output function which indicates “print formatted”.

12. What is scanf?

Scanf is an input function which is used to read input from the user.

Leave a Reply

Your email address will not be published. Required fields are marked *