Palindrome program

Write a program that reads a word into a C-string (a character array). The program should then determine whether the word would be a palindrome if we remove the first character and add it at the back of the word. Use only C-string functions and C-strings.

so for example
Banana
if we take the B and add it to the back , ananaB it will become a Palindrome.
Can anyone help with this please?
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

#include <stdio.h>
#include <string.h>

int main(){
    char string1[20];
    int i, length;
    int flag = 0;

    printf("Enter a string: ");
    scanf("%s", string1);

    length = strlen(string1);

    for(i=0;i < length ;i++){
        if(string1[i] != string1[length-i-1]){
            flag = 1;
            break;
           }
        }

    if (flag) {
        printf("%s is not a palindrome ", string1);
    }
    else {
        printf("%s is a palindrome ", string1);
    }
    return 0;
}


This is what I got so far, its not entirely correct but atleast it can read if its palindrome or not
ananaB is not a palindrome.

If you don't remove the first character then it will become BananaB which is a palindrome.

...if we remove the first character and add it at the back of the word


Why don't you do it ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main() {
	char string1[20] = {0};

	printf("Enter a string: ");
	scanf("%18s", string1);

	const size_t length = strlen(string1);

	string1[length] = string1[0];
	int flag = 0;

	for (size_t i = 1; !flag && i <= length; ++i)
		if (string1[i] != string1[length - i + 1])
			flag = 1;

	printf("%s is %sa palindrome\n", string1 + 1, (flag ? "not " : ""));
}



Enter a string: qwe
weq is not a palindrome

Enter a string: qqww
qwwq is a palindrome

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

bool isPalindrome( char *s )
{
   int N = strlen( s );
   for ( char *p = s, *q = s + N - 1; p < q; p++, q-- ) if ( tolower( *p ) != tolower( *q ) ) return false;
   return true;   
}

int main()
{
   char str[20];
   cout << "Enter a string: ";
   cin >> str;
   cout << str << str[0] << ( isPalindrome( str + 1 ) ? " is " : " is not " ) << "a palindrome\n";
}


Enter a string: BaNanA
BaNanAB is a palindrome
Removing the first character in a string and pushing onto the back of the string doesn't automatically transform a non-palindrome into is-a-palindrome.

ananaB is still not a palindrome, a word or phrase that reads the same forwards and backwards.

Copy the first letter in the string and appending it to the end can make the word a palindrome, or it might not.
George P wrote:
ananaB is still not a palindrome, a word or phrase that reads the same forwards and backwards.
While you are right regarding the same it would be a palindrome to the original.

Consider this:
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
#include <stdio.h>
#include <string.h>

int main(){
    char string1[20];
    char string2[20]; // Note
    int i, length;
    int flag = 0;

    printf("Enter a string: ");
    scanf("%s", string1);

    length = strlen(string1);

strcpy (string2, string1 + 1); // Note
string2[length - 1] = string1[0]; // Note


    for(i=0;i < length ;i++){
        if(string1[i] != string2[length-i-1]){ // Note: string2
            flag = 1;
            break;
           }
        }

    if (flag) {
        printf("%s is not a palindrome ", string1);
    }
    else {
        printf("%s is a palindrome ", string1);
    }
    return 0;
}
This is an interesting question. I wrote a code which seems clever to me because it compares the first half of a word with its second part starting at the end. Maybe it is too complicated for a beginner because of the lower case conversion or the "short" boolean at end. However it works fine as expected. I hope it helps you ++

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
#include <iostream>
#include <algorithm>
#include <string>

bool isPalindrome(std::string word)
{
    char letterFromStart; // char starting at the beginning
    char letterFromEnd;   // char starting from the end of the string

    for (int i = 0; i < (word.length() / 2); i++)
    {
        letterFromStart = word[i];
        letterFromEnd = word[(word.length() - 1) - i];

        if (letterFromStart != letterFromEnd) return false;
    }
    return true;
}

bool isPalindrome(std::string word);

int main()
{   // some words
    std::string words[4] = { "Level", "rAbbit", "raDar", "poTAtoe" };
    // convert each word to lower case
    for (std::string& s : words)
        std::for_each(s.begin(), s.end(), [](char& c) { c = std::tolower(static_cast<unsigned char>(c)); });
    // check all words in our array
    for (int i = 0; i < *(&words + 1) - words; i++)
        if (isPalindrome(words[i]) ? std::cout << words[i] << " is a palindrome" << std::endl 
                                   : std::cout << words[i] << " is NOT a palindrome" << std::endl);
    return 0;
}

level is a palindrome
rabbit is NOT a palindrome
radar is a palindrome
potatoe is NOT a palindrome
Last edited on
Such a strange assignment. I don't expect to many words would turn into palindromes by moving the first letter to the back... eel is one. Any others?
Not really. It's just an exercise in c-string manipulation...
Adding the first letter to the end means that the first and last letters are always panindromic. So you can instead just check if the word is a palindrome starting with the second letter.
Last edited on
Adding for first letter to the end

The exercise as given by the OP is not adding the first letter to the end, it is removing the first letter and putting it at the end.

The exercise is then to determine if the rearranged word is a palindrome.
Last edited on
Registered users can post here. Sign in or register to post.