如何检查字符串是否是回文无法使用C ++中的字符串函数 [英] How to check if string is palindrome WITHOUT using string functions in C++

查看:190
本文介绍了如何检查字符串是否是回文无法使用C ++中的字符串函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是创建一个程序来检查输入的字符串是否是一个回文,所以我有这个简单的程序在这里:

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

int main()
{
char y [100],x [100];
cout<< 输入字< endl;
cin>> y;
strcpy(x,y);
strrev(x);
if(strcmp(y,x)== 0)
cout<< Palindrome;
else
cout<< 不是Palindrome;
system(pause);
return 0;但是,我不允许使用strcpy,strrev,strcmp等字符串函数,但是我不能使用字符串函数,如strcpy,strrev,strcmp等等。在这种情况下,我可能使用一个字符数组为这个程序。我可能会欣赏的代码是容易理解,因为我是C ++的初学者。任何帮助是赞赏。



***感谢早期的帮助,我完成了大部分的程序。



***我忘了添加,程序忽略字符串中的空格,如rad ar或race car将仍然作为回文返回。遗憾的是,我无法弄清楚这个空格检查函数的编码。

解决方案

回文只是一个单词,等于最后一个,等等。所以,为了检查它是否是一个回文,你只需要复制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;
}

这篇关于如何检查字符串是否是回文无法使用C ++中的字符串函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆