Check if two strings are same or not without using library functions
Last Updated :
20 Mar, 2023
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>
int len( char * S)
{
int i = 0;
while (S[i])
i++;
return i;
}
bool compareStrings( char S1[], char S2[])
{
if (len(S1) != len(S2))
return false ;
int i = 0;
while (S1[i]) {
if (S1[i] != S2[i])
return false ;
i++;
}
return true ;
}
int main()
{
char S1[] = "GeeksForGeeks" ;
char S2[] = "GeeksForGeeks" ;
bool ans = compareStrings(S1, S2);
printf ( "%s" , ans ? "True" : "False" );
return 0;
}
|
C++
#include <iostream>
#include <cstring>
using namespace std;
int len( char * S)
{
int i = 0;
while (S[i])
i++;
return i;
}
bool compareStrings( char S1[], char S2[])
{
if (len(S1) != len(S2))
return false ;
int i = 0;
while (S1[i]) {
if (S1[i] != S2[i])
return false ;
i++;
}
return true ;
}
int main()
{
char S1[] = "GeeksForGeeks" ;
char S2[] = "GeeksForGeeks" ;
bool ans = compareStrings(S1, S2);
cout << (ans ? "True" : "False" );
return 0;
}
|
Java
public class Main {
public static int len(String S) {
int i = 0 ;
while (i < S.length())
i++;
return i;
}
public static boolean compareStrings(String S1, String S2) {
if (len(S1) != len(S2))
return false ;
int i = 0 ;
while (i < S1.length()) {
if (S1.charAt(i) != S2.charAt(i))
return false ;
i++;
}
return true ;
}
public static void main(String[] args) {
String S1 = "GeeksForGeeks" ;
String S2 = "GeeksForGeeks" ;
boolean ans = compareStrings(S1, S2);
System.out.println(ans ? "True" : "False" );
}
}
|
Python3
def compareStrings(S1, S2):
if ( len (S1) ! = len (S2)):
return False
i = 0
while (i < len (S1)):
if (S1[i] ! = S2[i]):
return False
i + = 1
return True
if __name__ = = '__main__' :
S1 = "GeeksForGeeks"
S2 = "GeeksForGeeks"
ans = compareStrings(S1, S2)
print ( "True" if ans else "False" )
|
Javascript
<script>
function len(S)
{
let i = 0;
while (i < S.length)
i++;
return i;
}
function compareStrings(S1,S2)
{
if (len(S1) != len(S2))
return false ;
let i = 0;
while (i < S1.length)
{
if (S1[i] != S2[i])
return false ;
i++;
}
return true ;
}
let S1 = "GeeksForGeeks" ;
let S2 = "GeeksForGeeks" ;
let ans = compareStrings(S1, S2);
document.write(ans ? "True" : "False" );
</script>
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int len( string S)
{
int i = 0;
while (i < S.Length)
i++;
return i;
}
static bool compareStrings( string S1, string S2)
{
if (len(S1) != len(S2))
return false ;
int i = 0;
while (i < S1.Length)
{
if (S1[i] != S2[i])
return false ;
i++;
}
return true ;
}
public static void Main()
{
string S1 = "GeeksForGeeks" ;
string S2 = "GeeksForGeeks" ;
bool ans = compareStrings(S1, S2);
Console.Write(ans ? "True" : "False" );
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...