Program to Compare Two Strings

Program to Compare Two Strings

  • Write a program to Compare Two Strings in C
  • Write a program to Compare Two Strings in C++
  • Write a program to Compare Two Strings in Python
  • Write a program to Compare Two Strings in PHP
  • Write a program to Compare Two Strings in Java
  • Write a program to Compare Two Strings in Java Script
  • Write a program to Compare Two Strings in C#

Explanation:

The logic to compare two strings involves checking whether the strings are:

  1. Equal: The strings are identical in length and content.
  2. Not Equal: The strings differ in length or content.
  3. Lexicographical Order (optional): Determine which string comes first alphabetically (used in sorting).

Logic Steps:

  1. Check Lengths:
    • If the lengths are different, the strings are not equal.
  2. Compare Characters:
    • Iterate through each character in both strings.
    • Compare corresponding characters one by one.
  3. Decide the Result:
    • If all characters match and the lengths are equal, the strings are equal.
    • If a mismatch is found, determine which string is lexicographically greater (if required).

Program to Compare Two Strings

#include <stdio.h>
#include <string.h>

int main() {
    char str1[100], str2[100];
    
    // Scan the two strings
    printf("Enter first string: ");
    fgets(str1, sizeof(str1), stdin);
    printf("Enter second string: ");
    fgets(str2, sizeof(str2), stdin);
    
    // Remove newline character if present
    str1[strcspn(str1, "\n")] = '\0';
    str2[strcspn(str2, "\n")] = '\0';
    
    // Compare the strings
    if (strcmp(str1, str2) == 0) {
        printf("The strings are equal.\n");
    } else {
        printf("The strings are not equal.\n");
    }
    
    return 0;
}

#include <iostream>
#include <string>

int main() {
    std::string str1, str2;
    
    // Scan the two strings
    std::cout << "Enter first string: ";
    std::getline(std::cin, str1);
    std::cout << "Enter second string: ";
    std::getline(std::cin, str2);
    
    // Compare the strings
    if (str1 == str2) {
        std::cout << "The strings are equal." << std::endl;
    } else {
        std::cout << "The strings are not equal." << std::endl;
    }
    
    return 0;
}

str1 = input("Enter first string: ")
str2 = input("Enter second string: ")

# Compare the strings
if str1 == str2:
    print("The strings are equal.")
else:
    print("The strings are not equal.")

<?php
// Scan the two strings
echo "Enter first string: ";
$str1 = trim(fgets(STDIN));
echo "Enter second string: ";
$str2 = trim(fgets(STDIN));

// Compare the strings
if ($str1 == $str2) {
    echo "The strings are equal.\n";
} else {
    echo "The strings are not equal.\n";
}
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Initialize scanner object for user input
        Scanner scanner = new Scanner(System.in);
        
        // Scan the two strings
        System.out.print("Enter first string: ");
        String str1 = scanner.nextLine();
        System.out.print("Enter second string: ");
        String str2 = scanner.nextLine();
        
        // Compare the strings
        if (str1.equals(str2)) {
            System.out.println("The strings are equal.");
        } else {
            System.out.println("The strings are not equal.");
        }
    }
}

let str1 = prompt("Enter first string: ");
let str2 = prompt("Enter second string: ");

// Compare the strings
if (str1 === str2) {
    console.log("The strings are equal.");
} else {
    console.log("The strings are not equal.");
}

using System;

class Program {
    static void Main() {
        // Scan the two strings
        Console.Write("Enter first string: ");
        string str1 = Console.ReadLine();
        Console.Write("Enter second string: ");
        string str2 = Console.ReadLine();
        
        // Compare the strings
        if (str1.Equals(str2)) {
            Console.WriteLine("The strings are equal.");
        } else {
            Console.WriteLine("The strings are not equal.");
        }
    }
}

List of All Programs