Check if two strings are same or not without using library functions Last Updated : 20 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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: TrueExplanation: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; } 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 OutputTrue Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if two strings are same or not without using library functions G geeksforgeeks user Follow Improve Article Tags : Strings C Language DSA Practice Tags : Strings Similar Reads Check if two strings are same or not Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity.Examples:Input: s1 = "abc", s2 = "abc" Output: Yes Input: s1 = "", s2 = "" Output: Yes Input: s1 = "GeeksforGeeks", s2 = "Geeks" Output: No Approach - By Using (==) in C++/Python/C#, eq 7 min read Check if two numbers are equal without using comparison operators The following are not allowed to use Comparison Operators String function Examples: Input : num1 = 1233, num2 =1233Output : Same Input : num1 = 223, num2 = 233Output : Not Same Method 1: The idea is to use the XOR operator. XOR of two numbers is 0 if the numbers are the same, otherwise non-zero. C++ 5 min read Check whether two strings contain same characters in same order Given two strings s1 and s2, the task is to find whether the two strings contain the same characters that occur in the same order. For example string "Geeks" and string "Geks" contain the same characters in same order. Examples: Input: s1 = "Geeks", s2 = "Geks" Output: Yes Input: s1 = "Arnab", s2 = 9 min read Check if two given strings are isomorphic to each other | Set 2 (Using STL) Given two strings str1 and str2, the task is to check if the two strings are isomorphic to each other. Two strings str1 and str2 are called isomorphic if there is a one-to-one mapping possible for every character of str1 to every character of str2 and all occurrences of every character in âstr1â map 6 min read Check if two same sub-sequences exist in a string or not Given a string, the task is to check if there exist two equal sub-sequences in the given string. Two sub-sequences are said to be equal if they have the same characters arranged in the same lexicographical order but the position of characters differs from that in the original string. Examples: Input 5 min read Check whether two strings are equivalent or not according to given condition Given two strings A and B of equal size. Two strings are equivalent either of the following conditions hold true: 1) They both are equal. Or, 2) If we divide the string A into two contiguous substrings of same size A1 and A2 and string B into two contiguous substrings of same size B1 and B2, then on 12 min read Python program to check if a string contains all unique characters To implement an algorithm to determine if a string contains all unique characters. Examples: Input : s = "abcd" Output: True "abcd" doesn't contain any duplicates. Hence the output is True. Input : s = "abbd" Output: False "abbd" contains duplicates. Hence the output is False. One solution is to cre 3 min read Check whether two strings are anagrams of each other using unordered_map in C++ Write a function to check whether two given strings are an Anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an Anagram of each other. Approach: Unordered Map can 3 min read Check if a string can be split into two substrings with equal number of vowels Given a string S, the task is to check if the string can be split into two substrings such that the number of vowels in both of them are equal. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = "geeks"Output: YesExplanation: Splitting the strings into substrings "ge" 15+ min read Meta Strings (Check if two strings can become same after a swap in one string) Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are not considered here as Meta strings.Examples: Input: s1 = "geeks" , s2 = "keegs"Output: TrueExplanat 5 min read Like