且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查字符串是否是回文无法使用C ++中的字符串函数

更新时间:2022-11-01 10:20:21

回文只是一个单词,等于最后一个,等等。所以,为了检查它是否是一个回文,你只需要复制strlen函数的功能来知道你必须比较第一个字符的字符的位置。



在C ++中,使用你的变量,这可以很容易地用一个while循环:

  int i = 0; 
//这是一个空的终止字符?
while(y [i])
{
//移动到下一个字符
i ++;
}

为了简化操作,可以放在一个函数中:

  int stringLength(char * input)
{
int i = 0;
while(input [i])
{
i ++;
}
return i;
}

现在你只需要循环输入,将第一个字符与最后,比较第二个字符和第二个字符,等等...你只需要记住,由于数组的工作方式,最后一个字符实际上在位置len-1。

  #include< iostream> // cout和cin需要的值

//返回ac样式字符串的长度
int stringLength(char * input)
{

int i = 0;
//这是一个空的结束符
while(input [i])
{
//不,检查下一个字符
i ++;
}
return i;
}

int main()
{
//从用户获取输入。
char input [100];
std :: cin>>输入;

//计算输入的长度
int length = stringLength(input);

//在位置长度是空终止字符,
//最后一个字符实际上在位置len - 1
int lastIndex = length - 1;

//存储是否找不到回文
bool isPalindrome = true;

//循环遍历字符串检查第一个字符是否等于
//最后一个,第二个到第二个最后等等...
for(int i = lastIndex; (input [i]!= input [lastIndex-i])
(b)
{
isPalindrome = false;
break;
}
}

//输出结果
if(isPalindrome)
{
std :: cout< Palindrome< std :: endl;
}
else
{
std :: cout< 不回文< std :: endl;
}

return 0;
}


I am tasked to create a program that will check if the entered string is a palindrome or not, so I have this simple program here:

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
    char y[100], x[100];
    cout << "Enter word" << endl;
    cin >> y;
    strcpy(x, y);
    strrev(x);
    if (strcmp(y, x) == 0)
        cout << "Palindrome";
    else
        cout << "Not Palindrome";
    system("pause");
    return 0;
}

However, I am not allowed to use string functions like strcpy,strrev,strcmp and such. In this case, I might use an array of characters for this program. I might appreciate if the code is easy to understand, as I'm a beginner in C++. Any help is appreciated.

***Thanks for the earlier help, I have finished most of the program.

***I forgot to add, the program ignores empty spaces in the string like "rad ar" or " race car" will still return as a palindrome. Regrettably, I can't figure out on coding this space-checking function.

A palindrome is just a word whose first character is equal to the last one, and so on. So, in order to check if it is a palindrome, you only need the copy the functionality of the strlen function to know the position of the character you have to compare the first character.

In C++, using your variables, this can be easily done with a while loop:

int i = 0;
// Is this a null terminating character?
while (y[i])
{
    // Move to the next character
    i++;
}

To make things easier, and truly be a drop-in for strlen, this could be put in a function:

int stringLength (char *input)
{
    int i = 0;
    while (input[i])
    {
        i++;
    }
    return i;
}

Now you just have to loop through the input, comparing the first character to the last, comparing the second character to the second last, and so on... You just have to remember that due to the way arrays work, the last character is actually at position len-1.

#include <iostream> // required for cout, and cin

// returns the length of a c style string
int stringLength(char *input)
{

    int i = 0;
    // Is this a null terminating character
    while (input[i])
    {
        // No, check the next character
        i++;
    }
    return i;
}

int main()
{
    // Get input from the user.
    char input[100];
    std::cin >> input;

    // Calculate the length of the input
    int length = stringLength(input);

    // At position length is the null terminating character,
    // the last character is actually at position len - 1
    int lastIndex = length - 1;

    // Stores whether of not we found a palindrome
    bool isPalindrome = true;

    // Loop through the string checking if the first character is equal to
    // the last, second to second last etc...
    for (int i = lastIndex; i >= length/2; i--)
    {
        // Check the palindrome condition
        if (input[i] != input[lastIndex - i])
        {
            isPalindrome = false;
            break;
        }
    }

    // Output the result
    if (isPalindrome)
    {
        std::cout << "Palindrome" << std::endl;
    }
    else
    {
        std::cout << "Not palindrome" << std::endl;
    }

    return 0;
}