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

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 = ”zambiatek”
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 stringint 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 notbool 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 Codeint 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 stringint 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 notbool 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 Codeint 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 approachpublic 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 notdef 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 Codeif __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;}// Inputlet S1 = "GeeksForGeeks";let S2 = "GeeksForGeeks";// Function Calllet 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 stringstatic 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 notstatic 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 Codepublic 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 |
Output
True
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



