How to compare 2 strings in c++

This C program is used to compare two strings by using strcmp() function.

strcmp() function compares two strings lexicographically, and it's declared in stdio.h.

Case 1: when the strings are equal, it returns zero. Case 2: when the strings are unequal, it returns the difference between ascii values of the characters that differ. a) When string1 is greater than string2, it returns positive value.

b) When string1 is lesser than string2, it returns negative value.

Syntax:

int strcmp (const char *s1, const char *s2);

Program:

#include<stdio.h> #include<string.h> int main() { char a[100], b[100]; printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b); if( strcmp(a,b) == 0 ) printf("Entered strings are equal.\n"); else printf("Entered strings are not equal.\n"); return 0; }

Program Output:

Explanation:

This program is used to compare whether two given strings are equal or not using a predefined function strcmp(). So first of all, you have to include the stdio header file using the "include" preceding # which tells that the header file needs to be process before compilation, hence named preprocessor directive. Also, you have to include the string.h header file. The string.h header classifies one variable type, one macro, and various functions to manipulate arrays of characters within your program.

Then you have to define the main() function and it has been declared as an int as it is going to return an integer type value at the end of the program. The inside the main() function, you have to take two character arrays name as a[] and b[] having 100 consecutive memory location in both. You have to use the printf() function to display a message - "Enter the first string" to the screen.

The statement gets(a); will fetch a set of characters the form of a string and store them in the array a[]. Similarly, the second string will also get fetched from the keyboard and stored in character array b[]. Now the next is the 'if' statement which will check the condition whether the values of the array a[] is and the values of array b[] after using strcmp() function gives the result as 0 or not. If yes, then the printf() will display "Entered strings are equal" otherwise display "Entered strings are not equal." The strcmp() returns the following:

  • when Return value < 0, indicates that str1 is less than str2.
  • when Return value > 0, indicates that str2 is less than str1.
  • when Return value = 0, indicates that str1 is equal to str2.

And lastly the return 0; statement is used to return an integer type value back to main().

report this ad

Page 2

This C program is used to change string case with and without using strlwr and strupr functions.

Lowercase using strlwr function

Program:

#include <stdio.h> #include <string.h> int main() { char string[] = "Strlwr in C"; printf("%s\n",strlwr(string)); return 0; }

Uppercase using strupr function

Program:

#include <stdio.h> #include <string.h> int main() { char string[] = "strupr in c"; printf("%s\n",strupr(string)); return 0; }

Lowercase without strlwr function

Program:

#include <stdio.h> void lower_string(char*); int main() { char string[100]; printf("Enter a string to convert it into lower case\n"); gets(string); lower_string(string); printf("Entered string in lower case is \"%s\"\n", string); return 0; } void lower_string(char *string) { while(*string) { if ( *string >= 'A' && *string <= 'Z' ) { *string = *string + 32; } string++; } }

Uppercase without strupr function

Program:

#include <stdio.h> void upper_string(char*); int main() { char string[100]; printf("Enter a string to convert it into upper case\n"); gets(string); upper_string(string); printf("Entered string in upper case is \"%s\"\n", string); return 0; } void upper_string(char *string) { while(*string) { if ( *string >= 'a' && *string <= 'z' ) { *string = *string - 32; } string++; } }

report this ad

C program to compare two strings – In this specific article, we will describe the various ways to compare two different strings in C programming.

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

The ways used in this particular piece are as follows:

  • Using Standard Method
  • Using Function
  • Using Recursion
  • Using String Library Function

A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.

In the photo uploaded above, an example is showcased. A string needs to be entered first.

The string that is entered in the first case is as follows:

string1: welcome

Then, enter the second string that you want to compare with the first one.

string2: welcome

As you can see, both the strings are absolutely equal in nature.

Thus, the numerous methods to do the same in C programming are as follows:

Using Standard Method

  1. Read the entered strings s1 and s2 using gets(String) function and initialize to s1,s2.

2) First, check the lengths of two strings. If lengths of two strings are equal then only we can compare the strings. Compare each element of string s1 with each element of string s2 as follows.

a) for loop iterates with the structure for(i=0;s2[i]!=’\0′;i++)

If the element of string s1 is equal to the element of string s2 then increase the c value by 1. Repeat this step until the last character of s2 becomes null.

b) If c value is equal to i value,i.e each character of s1 is equal to each character of s2. Then print both the strings are equal. Otherwise print both the strings are not equal.

3) If the lengths of the two strings are not equal then print both the strings are not equal.

Using Standard Method

#include <stdio.h> #include <string.h> int main() { char s1[1000],s2[1000]; int i,c=0; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); if(strlen(s1)==strlen(s2)) { for(i=0;s2[i]!='\0';i++) { if(s1[i]==s2[i]) c++; } if(c==i) printf("strings are equal"); else printf("strings are not equal"); } else printf("strings are not equal"); return 0; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

#include <stdio.h>

#include <string.h>

int main()

{

    char s1[1000],s2[1000];  

    int i,c=0;

    printf("Enter  string1: ");

    gets(s1);

    printf("Enter  string2: ");

    gets(s2);

    if(strlen(s1)==strlen(s2))

    {

     for(i=0;s2[i]!='\0';i++)  

        {

         if(s1[i]==s2[i])

         c++;

    }

    if(c==i)

          printf("strings are equal");

        else

          printf("strings are not equal");

    }

    else

     printf("strings are not equal");

    return 0;

}

Output:

Output

Enter string1: goutham Enter string2: goutham strings are equal Enter string1: hello Enter string2: world strings are not equal

Enter  string1: goutham

Enter  string2: goutham

strings are equal

Enter  string1: hello

Enter  string2: world

strings are not equal

Using Function

  1. The main() function calls the stringcompare(char *s1,char *s2) function to compare the two strings.

2) If the length of the two strings is equal then,

a) compare each character of the string s1 with each character of the string s2 using for loop with the structure for(i=0;s2[i];i++).

b) If a character of string s1 is equal to a character of string s2 then increase the c value. Repeat till the last element of the string s1.

If c is equal to i then print both the strings are equal.

3) If the lengths of two strings are not equal then print two strings are not equal.

C Program To Compare Two Strings Using Function

#include <stdio.h> #include <string.h> int stringcompare(char *s1,char *s2) { int i,c=0; if(strlen(s1)==strlen(s2)) { for(i=0;s2[i];i++) { if(s1[i]==s2[i]) c++; } if(c==i) return 1; } return 0; } int main() { char s1[1000],s2[1000],c; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); c=stringcompare(s1,s2); if(c) printf("strings are equal"); else printf("strings are not equal"); return 0; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#include <stdio.h>

#include <string.h>

int stringcompare(char *s1,char *s2)

{

int i,c=0;

if(strlen(s1)==strlen(s2))

    {

     for(i=0;s2[i];i++)  

        {

         if(s1[i]==s2[i])

         c++;

    }

    if(c==i)

          return 1;

    }

    return 0;

}

int main()

{

    char s1[1000],s2[1000],c;  

    printf("Enter  string1: ");

    gets(s1);

    printf("Enter  string2: ");

    gets(s2);

    c=stringcompare(s1,s2);

    if(c)

        printf("strings are equal");

    else

        printf("strings are not equal");

return 0;

}

Output:

Output

Enter string1: computer Enter string2: programming strings are not equal Enter string1: c programming Enter string2: c programming strings are equal

Enter  string1: computer

Enter  string2: programming

strings are not equal

Enter  string1: c programming

Enter  string2: c programming

strings are equal

Using Recursion

  1. The function stringcompare() calculates the lengths of two strings as l1=strlen(s1) and l2=strlen(s2).

2) If the length of string s1 is not equal to the length of string s2 then the function stringcompare() returns 0.

3) If two strings lengths are equal,

a) Then compare s1[i] with s2[i],if s1[i] equal to s2[i] then increase c value,i value.

b) The function calls itself as stringcompare(s1,s2).It calls itself until i<l1 becomes false.

4) If c is equal to i then this function returns 1 otherwise it returns 0.

5) If c=1 then main function prints two strings are equal,if c=0 it prints two strings are not equal.

C Program To Compare Two Strings Using Recursion

#include <stdio.h> #include <string.h> int stringcompare(char *s1,char *s2) { static int i=0,l1=strlen(s1),l2=strlen(s2),c=0; if(l1!=l2) { return 0; } else if(i<l1) { if(s1[i]==s2[i]) c++; i++; stringcompare(s1,s2); } if(c==i) return 1; return 0; } int main() { char s1[1000],s2[1000],c; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); c=stringcompare(s1,s2); if(c) printf("strings are equal"); else printf("strings are not equal"); return 0; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#include <stdio.h>

#include <string.h>

int stringcompare(char *s1,char *s2)

{

static int i=0,l1=strlen(s1),l2=strlen(s2),c=0;

    if(l1!=l2)

    {

       return 0;

}  

else if(i<l1)

{

         if(s1[i]==s2[i])

            c++;

            i++;

stringcompare(s1,s2);

    }

    if(c==i)

    return 1;

    return 0;

}

int main()

{

    char s1[1000],s2[1000],c;  

    printf("Enter  string1: ");

    gets(s1);

    printf("Enter  string2: ");

    gets(s2);

    c=stringcompare(s1,s2);

    if(c)

        printf("strings are equal");

    else

        printf("strings are not equal");

return 0;

}

Output:

Output

Enter string1: hello Enter string2: hello strings are equal

Enter  string1: hello

Enter  string2: hello

strings are equal

Using String Library Function
  1. The function strcmp(String1, String2) is the string library function to compare two strings. If both the strings are equal then it returns 1 otherwise it returns 0.

2) The function strcmp(String1, String2) is available in the string.h header file.

Using String Library Function

#include <stdio.h> #include <string.h> int main() { char s1[1000],s2[1000]; printf("Enter string1: "); gets(s1); printf("Enter string2: "); gets(s2); if(!strcmp(s1,s2)) printf("strings are equal"); else printf("strings are not equal"); return 0; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include <stdio.h>

#include <string.h>

int main()

{

   char s1[1000],s2[1000];  

    printf("Enter  string1: ");

    gets(s1);

    printf("Enter  string2: ");

    gets(s2);

    if(!strcmp(s1,s2))

        printf("strings are equal");

    else

        printf("strings are not equal");    

    return 0;

}

Output:

Output

Enter string1: welcome Enter string2: welcome strings are equal

Enter  string1: welcome

Enter  string2: welcome

strings are equal

Última postagem

Tag