Skip to content
Related Articles

Related Articles

Improve Article

Check if two strings are same or not without using library functions

  • Last Updated : 05 Jul, 2021

Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions.

Examples:

Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksForGeeks”
Output: 
True
Explanation:
S1 and S2 are the same strings

Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksforGeeks”
Output: 
False

 

Approach: Follow the steps below to solve the problem:



  • Create a function compareStrings() that takes the two strings S1 and S2 as input parameters and does the following:
    • If the lengths of S1 and S2 are different, return false.
    • Store the length of S1 in a variable, say N.
    • Traverse from 0 to N-1 using the variable i and do the following:
      • If S1[i] is not equal to S2[i], return false.
    • Return true at the end of the traversal.

Below is the implementation of the above approach:

C




#include <stdbool.h>
#include <stdio.h>
 
// Function to calculate length of string
int len(char* S)
{
    // Variable for traversal
    int i = 0;
   
    // Traverse till null is reached
    while (S[i])
        i++;
   
    return i;
}
 
// Function to check whether
// two strings are same or not
bool compareStrings(char S1[], char S2[])
{
    // If lengths of the two
      // strings are different
    if (len(S1) != len(S2))
        return false;
   
    // Variable for traversal
    int i = 0;
   
    // Traverse till null is reached
    while (S1[i]) {
       
        if (S1[i] != S2[i])
            return false;
       
          // Increment i
        i++;
    }
    return true;
}
 
// Driver Code
int main()
{
    // Input
    char S1[] = "GeeksForGeeks";
    char S2[] = "GeeksForGeeks";
 
    // Function Call
    bool ans = compareStrings(S1, S2);
   
    printf("%s", ans ? "True" : "False");
  
    return 0;
}

Python3




# Function to check whether
# two strings are same or not
def compareStrings(S1, S2):
     
    # If lengths of the two
    # strings are different
    if (len(S1) != len(S2)):
        return False
 
    # Variable for traversal
    i = 0
 
    # Traverse till null is reached
    while (i < len(S1)):
        if (S1[i] != S2[i]):
            return False
 
        # Increment i
        i += 1
         
    return True
 
# Driver Code
if __name__ == '__main__':
 
    # Input
    S1 = "GeeksForGeeks"
    S2 = "GeeksForGeeks"
 
    # Function Call
    ans = compareStrings(S1, S2)
 
    print("True" if ans else "False")
 
# This code is contributed by mohit kumar 29

C#




using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to calculate length of string
static int len(string S)
{
    // Variable for traversal
    int i = 0;
   
    // Traverse till null is reached
    while (i < S.Length)
        i++;
   
    return i;
}
 
// Function to check whether
// two strings are same or not
static bool compareStrings(string S1, string S2)
{
     
    // If lengths of the two
    // strings are different
    if (len(S1) != len(S2))
        return false;
   
    // Variable for traversal
    int i = 0;
   
    // Traverse till null is reached
    while (i < S1.Length)
    {
        if (S1[i] != S2[i])
            return false;
             
        // Increment i
        i++;
    }
    return true;
}
 
// Driver Code
public static void Main()
{
     
    // Input
    string S1 = "GeeksForGeeks";
    string S2 = "GeeksForGeeks";
 
    // Function Call
    bool ans = compareStrings(S1, S2);
   
    Console.Write(ans ? "True" : "False");
}
}
     
// This code is contributed by SURENDRA_GANGWAR

Javascript




<script>
 
function len(S)
{
    // Variable for traversal
    let i = 0;
    
    // Traverse till null is reached
    while (i < S.length)
        i++;
    
    return i;
}
 
function compareStrings(S1,S2)
{
    // If lengths of the two
    // strings are different
    if (len(S1) != len(S2))
        return false;
    
    // Variable for traversal
    let i = 0;
    
    // Traverse till null is reached
    while (i < S1.length)
    {
        if (S1[i] != S2[i])
            return false;
              
        // Increment i
        i++;
    }
    return true;
}
 
// Input
let S1 = "GeeksForGeeks";
let S2 = "GeeksForGeeks";
 
// Function Call
let ans = compareStrings(S1, S2);
 
document.write(ans ? "True" : "False");
 
 
// This code is contributed by patel2127
</script>
Output
True

Time Complexity: O(N)
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :