How to check if string is palindrome WITHOUT using string functions in C++ [closed]

你说的曾经没有我的故事 提交于 2019-12-13 09:56:06

问题


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.


回答1:


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;
}



回答2:


Here ya go:

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

int main(){
    char y[100], x[100];
    cout<<"Enter word"<<endl;
    cin>>y;

    //Get the size of the word entered
    int len = 0;
    char*p = y;
    while(*p++) len++;

    //Check for palindrome
    bool palindrome=true;
    for(int i=0; i<len/2; ++i){
            if(y[i]!=y[len-1-i]) palindrome=false;
    }
    cout << "palindrome:" << (palindrome?"true":"false") << "\n";
    return 0;
}


来源:https://stackoverflow.com/questions/34257983/how-to-check-if-string-is-palindrome-without-using-string-functions-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!