answersLogoWhite

0


Best Answer

bool is_palindrome (char* str) {

if (str==0) return false;

int len = strlen (str);

if (len==0) return false;

int len2 = 0;

char* str2 = malloc (len + 1);

char* begin;

char* end;

char c;

for (int i=0; i<len; ++i) {

c = tolower (str[i]);

if ((c>='a' c<='z') (c>='0' c<='9')) str2[len2++] = c;

}

begin = str2;

end = begin+len2-1;

while ((*begin==*end) && (begin<end)) { ++begin; --end; }

free str2;

result = end<=begin;

}

User Avatar

Wiki User

6y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

int first,last,flag=1;

char str[100];

clrscr();

printf("Enter number to get to check wheather palindrome or not");

gets(str);

first=0;

last=strlen(str)-1;

printf("%d",first);

printf("%d",last);

while(first<=last)

{

if(str[first]!=str[last])

flag=0;

first++;

last--;

}

if(flag==1)

{

clrscr();

printf("this is palindrome");

getch();

}

else

{

clrscr();

printf("sorry this is not a palindrome");

getch();

}

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program.

A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome.

The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes.

The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string.

The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not.

The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in.

You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes.

#include<iostream>

#include<string>

#include<vector>

using namespace std;

string input_string(string prompt)

{

cout<<prompt<<":\t";

string input;

getline(cin, input, '\n');

return(input);

}

void convert_tolower(string& s)

{

for(string::iterator i=s.begin(); i!=s.end(); ++i)

*i=tolower(*i);

}

vector<int> strip(const string& s)

{

vector<int> v;

v.push_back(-1);

for(int i=0; i<s.size(); ++i)

{

if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9'))

{

v.push_back(i);

v.push_back(-1);

}

}

return(v);

}

bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y)

{

for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y)

if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] ))

break;

++x, --y;

if( v[x]==-1 )

++x, --y;

return(x>=0 && x<y && y-x>1);

}

int main()

{

string input;

while(1)

{

input=input_string("Enter a string");

if(input.size()==0)

break;

string copy(input);

convert_tolower(copy);

vector<int> v=strip(copy);

string pal;

int pos=0;

for(int i=0; i<v.size(); ++i)

{

int start=0, end=0;

if( ispalindrome( copy, v, i, start, end))

{

string tmp( input.substr(v[start],v[end]-v[start]+1));

if( tmp.size() > pal.size() )

{

pal = tmp;

pos = v[start];

}

}

}

if( pal.size() )

{

cout<<"Palindrome:\t";

for(int i=0; i<pos; ++i)

cout<<" ";

cout<<pal<<"\n"<<endl;

}

else

cout<<"The string contains no palindromes!\n"<<endl;

}

return(0);

}

Example output:

Enter a string: Madam, I'm Adam

Palindrome: Madam, I'm Adam

Enter a string: Madam, I'm Adam is a palindrome

Palindrome: Madam, I'm Adam

Enter a string: In girum imus nocte et consumimur igni

Palindrome: In girum imus nocte et consumimur igni

Enter a string: 0123456765432

Palindrome: 23456765432

Enter a string:

Press any key to continue . . .

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include

/*
Here's the basic algorithm below. Note that it assumes there's enough space in the "foo" array to fit the entire string.
*/
...

n = m = strlen(foo);
while(n > 0){
n--;

foo[m] = foo[n];
m++;

}
foo[m] = (char)NULL;

...


This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<string>

#include<algorithm>

bool is_palindrome (std::string s)

{

std::transform (s.begin(), s.end(), s.begin(), ::tolower);

std::string valid {"abcdefghijklmnopqrstuvwxyz1234567890"};

size_t pos;

while ((pos = s.find_first_not_of(valid)) != s.npos)

s.erase(pos);

size_t left = 0, right = s.size()-1;

while (left<right && s[left] == s[right])

++left, --right;

return right<=left;

}

void test (const std::string& s)

{

std::cout << "String"" << s << '"' << std::endl;

std::cout << "is " << (!is_palindrome (s) ? "not " : "") << "a palindrome\n" << std::endl;

}

int main()

{

test ("Madam, I'm Adam.");

test ("ABCDEFGfedcba!");

test ("Anonymous!");

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program.

A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome.

The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes.

The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string.

The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not.

The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in.

You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes.

#include<iostream>

#include<string>

#include<vector>

using namespace std;

string input_string(string prompt)

{

cout<<prompt<<":\t";

string input;

getline(cin, input, '\n');

return(input);

}

void convert_tolower(string& s)

{

for(string::iterator i=s.begin(); i!=s.end(); ++i)

*i=tolower(*i);

}

vector<int> strip(const string& s)

{

vector<int> v;

v.push_back(-1);

for(int i=0; i<s.size(); ++i)

{

if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9'))

{

v.push_back(i);

v.push_back(-1);

}

}

return(v);

}

bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y)

{

for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y)

if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] ))

break;

++x, --y;

if( v[x]==-1 )

++x, --y;

return(x>=0 && x<y && y-x>1);

}

int main()

{

string input;

while(1)

{

input=input_string("Enter a string");

if(input.size()==0)

break;

string copy(input);

convert_tolower(copy);

vector<int> v=strip(copy);

string pal;

int pos=0;

for(int i=0; i<v.size(); ++i)

{

int start=0, end=0;

if( ispalindrome( copy, v, i, start, end))

{

string tmp( input.substr(v[start],v[end]-v[start]+1));

if( tmp.size() > pal.size() )

{

pal = tmp;

pos = v[start];

}

}

}

if( pal.size() )

{

cout<<"Palindrome:\t";

for(int i=0; i<pos; ++i)

cout<<" ";

cout<<pal<<"\n"<<endl;

}

else

cout<<"The string contains no palindromes!\n"<<endl;

}

return(0);

}

Example output:

Enter a string: Madam, I'm Adam

Palindrome: Madam, I'm Adam

Enter a string: Madam, I'm Adam is a palindrome

Palindrome: Madam, I'm Adam

Enter a string: In girum imus nocte et consumimur igni

Palindrome: In girum imus nocte et consumimur igni

Enter a string: 0123456765432

Palindrome: 23456765432

Enter a string:

Press any key to continue . . .

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

// Palindrome.C : A palindrome is a string that is spelled the same way forward and backward.

// This code verifies the string entered IGNORING CAPITALIZATIONS, SPACES and PUNCTUATIONS.

// Most of the CRT library functions used are with security enhancements optimized for MS Visual C++ [2010].

// For convenience, remove the "/*.*/" during coding. Same with others, I'm using this to emphasize spacing.

// Removing this does not affect the flow of the program (since /**/ is designed for comment only.)

#include <ctype.h>

#include <conio.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define SIZE 0x80 // hex for 128.

int main(void)

{

/**/ char *strsrc=(char*)calloc(SIZE,sizeof(char)),*strver=(char*)calloc(SIZE,sizeof(char));

/**/ register size_t count,counter; bool ver;

/**/ printf_s("Enter String: "); gets_s(strsrc,SIZE);

/**/ for(count=0,counter=0;count<strlen(strsrc)+1;count++)

/*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count));

/**/ for(count=0,counter=strlen(strver)-1;count<strlen(strver);count++,counter--)

/*.....*/ *(strver+counter)==*(strver+count)?ver=true:ver=false;

/**/ printf_s("%s %s a palindrome.",strsrc,ver?"is":"is not");

/**/ _getch(); free(strsrc); free(strver); return 0;

}

// It is suggested to use MS Visual C++. Otherwise, remove "_s" in function names and comply with the

// appropriate passing parameters and other codings for other compilers.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Start by pointing at both ends of the string and compare the character values. While the characters match, move the pointers towards the middle, one character at a time, comparing as you go. If at any point the characters do not match, the string is not a palindrome. But if the pointers meet in the middle or pass each other, the string is a palindrome.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Without using 'string.h', here are the steps:

  1. On your main function, create a character array with the desired length.
  2. Invoke 'scanf()' or 'gets()' to read the entered word of the user.
  3. Create a loop that will count the length of the input string.
  4. Create a loop that will stop when the length of the string / 2 has been reached.
  5. If there are any dissimilar characters, that means they are not palindrome

#include


int

main(void)

{

char input[1024] = {"\0"};

int palindrome = 1;

int length = 0;

int count = 0;

// User input

printf("Enter a word: ");

scanf("%s", input);

// Count the length of the string

while (input[length] != '\0') {

length++;

}

// Check if it is palindrome

for (count = 0; count < (length / 2); count++) {

if (input[count] != input[length - count - 1]) {

palindrome = 0;

break;

}

}

if (palindrome) {

printf ("String is palindrome!");

} else {

printf ("String is not palindrome!");

}

return (0);

}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

This program reverses an integer (entered by the user) using while loop. Then, if statement is used to check whether the reversed number is equal to the original number or not.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Code for palindrome checking in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Code for creating pascal's triangle in C programming language?

code for creating pascal's triangle in C programming language?


Getting source code of c?

C is a programming language, so it doesn't have source code.(On the other hand, C compilers do have source code, but you, as a beginner in programming, could not understand them.)


Where can one find C programming tutorials?

You can find C programming tutorials online at the C programming website. They provide both free and paid tutorials for many aspects of the C and C++ code.


What does generic programming in c plus plus means?

Generic programming means that the code is generic enough that it compile on any C++ implementation. That is, it has no platform-specific code.


What is c and why you use?

C is a programming language, and it's mostly used for Systems Programming. Most kernels these days have at least some C code in them.


Microsoft office's source code written by which programming language?

C++


How Create mouse in C programming source code?

Here is the article for Mouse programming in C. In this link full details are given nicely.http://electrofriends.com/articles/computer-science/c-tutorials/mouse-programming-in-cc/


What is programming languages in c plus plus?

Programming in C++ means designing and writing a computer program using the C++ programming language. C++ is a high-level, machine-independent language which must be converted into machine-dependent code by a compiler.


What is Turbo C download used for?

Turbo C is a compiler for a general purpose computer programming language called C. It transforms code written in C into the computer language needed for executable programming.


C programming source code for push down automata?

i cant sorry


What do you mean by c taken programming in c?

A C program is a computer program written using the C programming language.


What translator is needed for C programming language?

To translate the C source code program, you need a program called a "C compiler".