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 stringsInput: S1 = ”GeeksForGeeks”, S2 = ”neveropen”
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; } |
C++
// C++ program for the above approach #include <iostream> #include <cstring> using namespace std; // 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); cout << (ans ? "True" : "False" ); return 0; } // This code is contributed by rishab |
Java
// Java program to implement the above approach public class Main { // Function to calculate length of string public 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 public static boolean 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.charAt(i) != S2.charAt(i)) return false ; // Increment i i++; } return true ; } // Driver Code public static void main(String[] args) { // Input String S1 = "GeeksForGeeks" ; String S2 = "GeeksForGeeks" ; // Function Call boolean ans = compareStrings(S1, S2); System.out.println(ans ? "True" : "False" ); } } // Contributed by adityashae15 |
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 |
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> |
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 |
True
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!