📌  相关文章
📜  不使用库函数检查两个字符串是否相同

📅  最后修改于: 2022-05-13 01:57:06.640000             🧑  作者: Mango

不使用库函数检查两个字符串是否相同

给定两个字符串S1S2 ,任务是在不使用字符串库函数的情况下检查它们是否相同。

例子:

解决方法:按照以下步骤解决问题:

  • 创建一个函数compareStrings() ,它将两个字符串S1S2作为输入参数并执行以下操作:
    • 如果S1S2的长度不同,则返回false
    • S1的长度存储在一个变量中,比如N
    • 使用变量i0遍历到N-1并执行以下操作:
      • 如果S1[i]不等于S2[i] ,则返回false
    • 在遍历结束时返回true

下面是上述方法的实现:

C
#include 
#include 
 
// 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


输出
True

时间复杂度: O(N)
辅助空间: O(1)