Program to Compare Two Strings

Description

* To compare two strings, each and every character is compared from both the strings.
* If str1 and str2 are strings, then comparing two strings will give,
  1) str1 and str2 are equal,
  2) str1 > str2, str1 will come after str2 in dictionary order,
  3) str1 < str2, str1 will come before str2 in dictionary order.

C/C++

/* C program to compare two strings */
//Save it as CompareTwoString.c

#include<stdio.h>
#include<string.h>
int main(){

    char str1[100], str2[100];
    int i=0, length1, length2, flag=0;

    printf("Enter first string : ");
    gets(str1);

    printf("Enter second string : ");
    gets(str2);

    
    //Calculating length of both the string
	
    length1 = strlen(str1);
    length2 = strlen(str2);

    if(length1 == length2){
        while(i<length1){
            if(str1[i] == str2[i]){
                i++;
            }
            else{
                break;
            }
        }
        if(i == length1){
            flag = 1;
            printf("\nThe strings are equal");
        }
    }
    else{
        printf("\nStrings are not equal");
    }

    
    //If length of strings are equal but characters are different
	
    if(flag == 0){
        if(str1[i]>str2[i]){
            printf("\nFirst string is greater than second string");
        }
        else if(str1[i]<str2[i]){
            printf("\Second string is greater than first string");
        }
    }

    
    //The library function strcmp(str1, str2) compares string str1 with str2
    //It is defined in string.h header file
    //Syntax : int strcmp (const char* str1, const char* str2);
    //It compares string character by character
	
    if(length1 == length2){
        if(strcmp(str1,str2) == 0){
            printf("\nThe strings are equal");
        }
        else if(strcmp(str1,str2) > 0){
            printf("\nFirst string is greater than second string");
        }
        else if(strcmp(str1,str2) < 0){
            printf("\nSecond string is greater than first string");
        }
    }
    else{
        printf("\nStrings are not equal");
    }

    return 0;
}
Input:
Enter first string : Hello
Enter second string : Hello

Output:
The strings are equal
The strings are equal

Java

/* Java program to compare two strings */
//Save it as CompareTwoString.java

import java.io.*;
import java.util.Scanner;

public class CompareTwoString {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		
		String str1, str2;
		
		System.out.println("Enter first string : ");
		str1 = scanner.nextLine();
		
		System.out.println("Enter second string : ");
		str2 = scanner.nextLine();
		
		
		/*
		   In java, Strings can be compared three ways
		   1) By equals() method
		   2) By = = operator
                   3) By compareTo() method
		 */
		
		
		/* By equals method */
		// equals() method compares the original content of the string. 
		
		System.out.println("Using equals() method : " + str1.equals(str2));
		
		//equalsIgnoreCase() compares this String to another string, ignoring case.
		
		System.out.println("Using equalsIgnoreCase() method : " + str1.equalsIgnoreCase(str2));
		
		
		/* By == operator */
		//The = = operator compares references not values.
		
		System.out.println("Using == operator : "+ (str1 == str2));
		
		
		/* By compareTo() method*/
		// compareTo() method compares values lexicographically
		/*
		   str1 == str2 :0
		   str1 > str2 : positive value
                   str1 < str2 : negative value
		*/
		
		System.out.println("Using compareTo() : " + str1.compareTo(str2));
	}
}
Input:
Enter first string : 
Hello
Enter second string : 
hello

Output:
Using equals() method : false
Using equalsIgnoreCase() method : true
Using == operator : false
Using compareTo() : -32

Related Programs

1) Program to Reverse a String
2) Find Substring of a Given String
3) Program to Insert a String into Another String
4) Program to check String is Palindrome
5) Program to copy string
6) Program to remove vowels from a String
7) Program to Multiply Numbers Present in a String
8) Program to remove given number from a string
9) Count Number of Words in a String
10) Program to Sort set of strings in alphabetical order
Share Me

Leave a Reply