Palindrome, no spaces

So I've written a program to check whether a sentence is a palindrome or not. I specifically used an array for that and the program is supposed to ignore spaces (and possibly the difference between normal and capital letters). I could only find on the web how to achieve this using strings.

Is it possible with arrays? I tried using delete[] etc. and it refuses to work properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int main()
{
    char willcontrol[200];

    cout<<"Enter a phrase: ";
    cin.getline(willcontrol, 200);

    int length;
    for(length = 0; willcontrol[length] != '\0'; length++);

    int ensign = 1;

    for(int i = 0, j = length-1; i <length/2; i++, j--)
    {
        if(willcontrol[i] != willcontrol[j])
        {
            ensign = 0;
            break;
        }
    }
    if(ensign)

        cout<<"It is a palindrome."<<endl;
        else
        cout<<"It is not a palindrome."<<endl;

    return main();

}
this is not allowed: return main();
see: http://en.cppreference.com/w/cpp/language/main_function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cctype> // std::isspace, std::tolower

int main()
{
    start_of_main:

        char willcontrol[200];

        std::cout<<"Enter a phrase: ";
        std::cin.getline( willcontrol, sizeof(willcontrol) );

        // determine the number of characters entered by the user
        // http://en.cppreference.com/w/cpp/io/basic_istream/gcount
        const std::size_t nchars = std::cin.gcount() - 1 ;
        if( nchars == 0 ) goto start_of_main ; // empty input; repeat

        bool ensign = true ;

        std::size_t left = 0 ; // initially beginning of string
        std::size_t right = nchars-1 ; // initially last character in string

        while( left < right && ensign )
        {
            // skip to next character if white space in encountered
            if( std::isspace( willcontrol[left] ) ) ++left ;
            else if( std::isspace( willcontrol[right] ) ) --right ;

            // compare the character at the left with that at the right ignoring case
            else if( std::tolower( willcontrol[left] ) == std::tolower( willcontrol[right] ) )
            {
                // palidrome so far, get to the next pair if characters
                ++left ;
                --right ;
            }
            else ensign = false ; // not a palindrome
        }

        if(ensign) std::cout << "It is a palindrome.\n" ;
        else
        {
            std::cout << "It is not a palindrome.\n" ;
            goto start_of_main ;
        }
}
Why did you put -1 to both size_t=nchars-1 and nchars = cin.gcount() - 1?

Isn't stating that only for gcount enough?
> Isn't stating that only for gcount enough?

With std::istream::getline(), the terminating null character is counted as an extracted character.
The count returned by gcount() includes the terminating null character;
and gcount()-1 gives us the length of the extracted c-style string.

If the string has N characters ( gcount()-1 == N ), the position of the last character would be N-1.
Thank you!
Sorry for being a little late,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>

using namespace std;
void reverses(char a[255])
{
    for(int i=0; i<int(strlen(a))/2; i++)
    {
        char temp=a[i];
        a[i]=a[int(strlen(a))-i-1];
        a[int(strlen(a))-i-1]=temp;
    }
}
int main()
{
    char a[255],a1[255];
    cin.getline(a,255);
    strcpy(a1,a);
    reverses(a1);
    if(strcmp(a,a1)==0)
        cout<<"Is palindrome"<<endl;
    else
        cout<<"Is not"<<endl;
    return 0;
}

Is shorter and works on spaces.
*works by reversing the string and test if they are the same*
Topic archived. No new replies allowed.