如何通过编程检查字符串是否对称

如果字符串的两半相同,则称该字符串是对称的。在本文中,您将学习一种算法来确定给定的字符串是否对称。您还将学习如何使用最流行的编程语言(如 C++、Python、C 和 JavaScript)实现此算法。

问题陈述

你得到一个字符串。您需要确定给定的字符串是否对称。

示例 1 :让 str = "abab"。

给定是对称的,因为字符串的两半是相同的。

因此,输出是“是的,给定的字符串是对称的”。

示例 2 :让 str = "madam"。

如果字符串的长度为奇数,则忽略字符串的中间字符。因此,第一半=“ma”,第二半=“am”。两半不一样。

因此,输出是“不,给定的字符串不对称”。

示例 3 :让 str = "madma"。

第一半 = "ma" 和第二半 = "ma"。字符串的两半是相同的。

因此,输出是“是的,给定的字符串是对称的”。

确定给定字符串是否对称的算法

您可以通过以下方法确定给定的字符串是否对称:

  1. 找出字符串的长度。
  2. 找到字符串的 midIndex。
    • 如果字符串的长度是偶数,则midIndex = length/2
    • 如果字符串的长度是奇数,则midIndex = (length/2) + 1 。在这种情况下,字符串的中间字符将被忽略以进行比较。
  3. 初始化两个指针变量pointer1pointer2pointer1将存储字符串第一个字符 (0) 的索引, pointer2将存储字符串中间字符 (midIndex) 的索引。
  4. 现在使用while循环比较字符串两半的相应字符。运行while循环,直到pointer1<midIndex 和 pointer2<lengthOfString
  5. 比较索引pointer1pointer2处的相应字符。
  6. 如果发现任何相应的字符不同,则返回false 。如果没有找到不同的对应字符,则返回true
  7. 此外,请确保在每次迭代中增加指针1指针 2的值。

确定给定字符串是否对称的 C++ 程序

下面是确定给定字符串是否对称的 C++ 程序:

 // C++ program to check whether the string is symmetrical or not
#include <iostream>
using namespace std;
// Function to check whether the string is symmetrical or not
bool isSymmetrical(string str)
{
int midIndex;
int length = str.length();
// If the length of string is even
if (length % 2 == 0)
{
midIndex = length/2;
}
// If the length of string is odd
else
{
midIndex = length/2 + 1;
}
int pointer1 = 0;
int pointer2 = midIndex;
while(pointer1<midIndex && pointer2<length)
{
if(str[pointer1] == str[pointer2])
{
pointer1 += 1;
pointer2 += 1;
}
else
{
return false;
}
}
return true;
}
// Driver Code
int main()
{
// Test case: 1
string str1 = "abab";
cout << "String 1: " << str1 << endl;
if (isSymmetrical(str1))
{
cout << "Yes, the given string is symmetrical" << endl;
}
else
{
cout << "No, the given string is not symmetrical" << endl;
}
// Test case: 2
string str2 = "madam";
cout << "String 2: " << str2 << endl;
if (isSymmetrical(str2))
{
cout << "Yes, the given string is symmetrical" << endl;
}
else
{
cout << "No, the given string is not symmetrical" << endl;
}
// Test case: 3
string str3 = "madma";
cout << "String 3: " << str3 << endl;
if (isSymmetrical(str3))
{
cout << "Yes, the given string is symmetrical" << endl;
}
else
{
cout << "No, the given string is not symmetrical" << endl;
}
// Test case: 4
string str4 = "civic";
cout << "String 4: " << str4 << endl;
if (isSymmetrical(str4))
{
cout << "Yes, the given string is symmetrical" << endl;
}
else
{
cout << "No, the given string is not symmetrical" << endl;
}
// Test case: 5
string str5 = "khokho";
cout << "String 5: " << str5 << endl;
if (isSymmetrical(str5))
{
cout << "Yes, the given string is symmetrical" << endl;
}
else
{
cout << "No, the given string is not symmetrical" << endl;
}
return 0;
}

输出:

 String 1: abab
Yes, the given string is symmetrical
String 2: madam
No, the given string is not symmetrical
String 3: madma
Yes, the given string is symmetrical
String 4: civic
No, the given string is not symmetrical
String 5: khokho
Yes, the given string is symmetrical

相关:如何在 C++、Python 和 JavaScript 中反转字符串

确定给定字符串是否对称的 Python 程序

以下是确定给定字符串是否对称的 Python 程序:

 # Python program to check whether the string is symmetrical or not
# Function to check whether the string is symmetrical or not
def isSymmetrical(str):
midIndex = 0
length = len(str)
if length%2 == 0:
midIndex = length//2
else:
midIndex = length//2 + 1
pointer1 = 0
pointer2 = midIndex
while pointer1<midIndex and pointer2<length:
if (str[pointer1] == str[pointer2]):
pointer1 += 1
pointer2 += 1
else:
return False
return True

# Test case: 1
str1 = "abab"
print("String 1:", str1)
if (isSymmetrical(str1)):
print("Yes, the given string is symmetrical")
else:
print("No, the given string is not symmetrical")
# Test case: 2
str2 = "madam"
print("String 2:", str2)
if (isSymmetrical(str2)):
print("Yes, the given string is symmetrical")
else:
print("No, the given string is not symmetrical")
# Test case: 3
str3 = "madma"
print("String 3:", str3)
if (isSymmetrical(str3)):
print("Yes, the given string is symmetrical")
else:
print("No, the given string is not symmetrical")
# Test case: 4
str4 = "civic"
print("String 4:", str4)
if (isSymmetrical(str4)):
print("Yes, the given string is symmetrical")
else:
print("No, the given string is not symmetrical")
# Test case: 5
str5 = "khokho"
print("String 5:", str5)
if (isSymmetrical(str5)):
print("Yes, the given string is symmetrical")
else:
print("No, the given string is not symmetrical")

输出:

 String 1: abab
Yes, the given string is symmetrical
String 2: madam
No, the given string is not symmetrical
String 3: madma
Yes, the given string is symmetrical
String 4: civic
No, the given string is not symmetrical
String 5: khokho
Yes, the given string is symmetrical

相关:学习Python?这是操作字符串的方法

判断给定字符串是否对称的 JavaScript 程序

下面是判断给定字符串是否对称的 JavaScript 程序:

 // JavaScript program to check whether the string is symmetrical or not
// Function to check whether the string is symmetrical or not
function isSymmetrical(str) {
var midIndex;
var length = str.length;
// If the length of string is even
if (length % 2 == 0) {
midIndex = Math.floor(length/2);
}
// If the length of string is odd
else {
midIndex = Math.floor(length/2) + 1;
}
var pointer1 = 0;
var pointer2 = midIndex;
while(pointer1<midIndex && pointer2<length) {
if(str[pointer1] == str[pointer2]) {
pointer1 += 1;
pointer2 += 1;
} else {
return false;
}
}
return true;
}

// Test case: 1
var str1 = "abab";
document.write("String 1: " + str1 + "<br>");
if (isSymmetrical(str1)) {
document.write("Yes, the given string is symmetrical" + "<br>");
} else {
document.write("No, the given string is not symmetrical" + "<br>");
}
// Test case: 2
var str2 = "madam";
document.write("String 2: " + str2 + "<br>");
if (isSymmetrical(str2)) {
document.write("Yes, the given string is symmetrical" + "<br>");
} else {
document.write("No, the given string is not symmetrical" + "<br>");
}
// Test case: 3
var str3 = "madma";
document.write("String 3: " + str3 + "<br>");
if (isSymmetrical(str3)) {
document.write("Yes, the given string is symmetrical" + "<br>");
} else {
document.write("No, the given string is not symmetrical" + "<br>");
}
// Test case: 4
var str4 = "civic";
document.write("String 4: " + str4 + "<br>");
if (isSymmetrical(str4)) {
document.write("Yes, the given string is symmetrical" + "<br>");
} else {
document.write("No, the given string is not symmetrical" + "<br>");
}
// Test case: 5
var str5 = "khokho";
document.write("String 5: " + str5 + "<br>");
if (isSymmetrical(str5)) {
document.write("Yes, the given string is symmetrical" + "<br>");
} else {
document.write("No, the given string is not symmetrical" + "<br>");
}

输出:

 String 1: abab
Yes, the given string is symmetrical
String 2: madam
No, the given string is not symmetrical
String 3: madma
Yes, the given string is symmetrical
String 4: civic
No, the given string is not symmetrical
String 5: khokho
Yes, the given string is symmetrical

相关:如何查找字符串中最常出现的字符

基于字符串解决问题

字符串是编程面试中最重要的话题之一。您必须解决一些基于字符串的著名编程问题,例如检查字符串是否为回文、检查两个字符串是否互为变位词、查找字符串中出现频率最高的字符、反转字符串等。重新寻找做好充分准备。