📌  相关文章
📜  检查字符串是否为 Colindrome

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

检查字符串是否为 Colindrome

给定一个字符串,检查它是否是 Colindrome。如果一个字符串有连续的 3 个字母,然后是这 3 个字母的倒数,则称该字符串为 colindrome。

例子 :

Input : cappaccappac
Output : String is colindrome

Input : mollomaappaa 
Output : String is Colindrome

方法:取两个空字符串s1 和 s2,并开始迭代给定的字符串。取字符串的前三个字母并将其存储在 s1 中,接下来的三个字符存储在 s2 中,然后匹配 s1 和 s2。如果它们相同,则再次执行相同操作,直到给定字符串的长度否则返回 false。

C++
// CPP program to check if a
// string is Colindrome or not
#include
using namespace std;
  
// Function to check if a string
// is Colindrome or not
bool colindrome(string s)
{
    int i1 = 0;
      
    for (int i = 0; i < s.length(); i++) {
      
        int i2 = i1 + 3;
      
        // Taking two empty Strings
        string s1 = "";
        string s2 = "";
      
        int c1 = 0, c2 = 0;
      
        // Iterate upto 3 letters
        for (i1 = i1; i1 < s.length(); i1++) {
            c1++;
  
            // concat each word with taken String
            s1 = s1 + s[i1];
            if (c1 == 3) {
                break;
            }
        }
  
        // Iterate upto 3 letters
        for (i2 = i2; i2 < s.length(); i2++) {
            c2++;
  
            // concat each word with taken String
            s2 = s2 + s[i2];
            if (c2 == 3) {
                break;
            }
        }
  
        // Reverse the second String
        string s3 = "";
        for (int k = s2.length() - 1; k >= 0; k--) {
            s3 = s3 + s2[k];
        }
  
        // Checks equality of two strings
        if (s1 != s3) {
            // If the two Strings are not same
            // then return false
            return false;
        }
  
        // Increment first variable by 6 and
        // second variable by 3
        i1 = i1 + 6;
        i2 = i2 + 3;
    }
      
    return true;
}
  
// Driver Code
int main()
{   
    // Input string
    string s = "cbbbbc";
      
    if(colindrome(s))
        cout<<"String is colindrome\n";
    else
        cout<<"Not ccolindrome";
          
    return 0;
}


Java
// Java code to check if a given string is Colindrome.
public class Colindrome {
  
    // Function to check Colindrome
    static boolean colindrome(String s)
    {
        int i1 = 0;
        for (int i = 0; i < s.length(); i++) {
            int i2 = i1 + 3;
  
            // Taking two empty Strings
            String s1 = "";
            String s2 = "";
            int c1 = 0, c2 = 0;
  
            // Iterate upto 3 letters
            for (i1 = i1; i1 < s.length(); i1++) {
                c1++;
  
                // concat each word with taken String
                s1 = s1 + s.charAt(i1);
                if (c1 == 3) {
                    break;
                }
            }
  
            // Iterate upto 3 letters
            for (i2 = i2; i2 < s.length(); i2++) {
                c2++;
  
                // concat each word with taken String
                s2 = s2 + s.charAt(i2);
                if (c2 == 3) {
                    break;
                }
            }
  
            // Reverse the second String
            String s3 = "";
            for (int k = s2.length() - 1; k >= 0; k--) {
                s3 = s3 + s2.charAt(k);
            }
  
            // Checks equality of two strings
            if (s1.equals(s3) != true) {
                // If the two Strings are not same then return false
                return false;
            }
  
            // Increment first variable by 6 and second variable by 3
            i1 = i1 + 6;
            i2 = i2 + 3;
        }
        return true;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String s = "cbbbbc";
        boolean b = colindrome(s);
        if (b) {
            System.out.println("String is colindrome");
        }
        else {
            System.out.println("Not Colindrome");
        }
    }
}


Python3
# python program to check if a
# string is Colindrome or not
  
# Function to check if a string
# is Colindrome or not
def colindrome(s):
    i1 = 0
    for i in range (0, len(s)):
        i2 = i1 + 3
  
        # Taking two empty Strings
        s1 = ""
        s2 = ""
      
        c1 = 0; c2 = 0;
      
        # Iterate upto 3 letters
        while(i1 < len(s)):
            c1 += 1
  
            # concat each word with taken String
            s1 = s1 + s[i1]
            if (c1 == 3) : break
            i1 += 1
  
        # Iterate upto 3 letters
        while(i2 < len(s)):
            c2 += 1
  
            # concat each word with taken String
            s2 = s2 + s[i2]
            if (c2 == 3) : break
            i2 += 1
  
        # Reverse the second String
        s3 = ""
        for k in range(len(s2)-1, -1, -1) :
            s3 = s3 + s2[k]
  
        # Checks equality of two strings
        if (s1 != s3) :
            # If the two Strings are not same
            # then return false
            return False
  
        # Increment first variable by 6 and
        # second variable by 3
        i1 = i1 + 6
        i2 = i2 + 3
  
    return True
  
# Driver Code
if __name__ == '__main__' : 
  
    # Input string
    s = "cbbbbc";
      
    if(colindrome(s)) :
        print("String is colindrome")
    else :
        print("Not ccolindrome")
  
# contributed by Abhishek Sharma DTU.


C#
// C# code to check if a given string is Colindrome.
using System;
  
public class Colindrome
{
  
    // Function to check Colindrome
    static bool colindrome(String s)
    {
        int i1 = 0;
        for (int i = 0; i < s.Length; i++) 
        {
            int i2 = i1 + 3;
  
            // Taking two empty Strings
            String s1 = "";
            String s2 = "";
            int c1 = 0, c2 = 0;
  
            // Iterate upto 3 letters
            for (i1 = i1; i1 < s.Length; i1++)
            {
                c1++;
  
                // concat each word with taken String
                s1 = s1 + s[i1];
                if (c1 == 3)
                {
                    break;
                }
            }
  
            // Iterate upto 3 letters
            for (i2 = i2; i2 < s.Length; i2++)
            {
                c2++;
  
                // concat each word with taken String
                s2 = s2 + s[i2];
                if (c2 == 3) 
                {
                    break;
                }
            }
  
            // Reverse the second String
            String s3 = "";
            for (int k = s2.Length - 1; k >= 0; k--)
            {
                s3 = s3 + s2[k];
            }
  
            // Checks equality of two strings
            if (s1.Equals(s3) != true)
            {
                // If the two Strings are not 
                // same then return false
                return false;
            }
  
            // Increment first variable by 6 and 
            // second variable by 3
            i1 = i1 + 6;
            i2 = i2 + 3;
        }
        return true;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        String s = "cbbbbc";
        bool b = colindrome(s);
        if (b)
        {
            Console.WriteLine("String is colindrome");
        }
        else 
        {
            Console.WriteLine("Not Colindrome");
        }
    }
}
  
// This code is contributed by PrinciRaj1992


输出:
String is colindrome