answersLogoWhite

0


Best Answer

cls

input "enter a string=";s$

l=len(s$)

for i = l to 1 step-1

c$=mid$(s$,i,l)

next i

r$=c$+r$

if r$=c$ then

print "no. is palindrome"

else

print "no. is not palindrome"

endif

end

User Avatar

Wiki User

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

Wiki User

15y ago

something like this:

static int check1 (const char *p, size_t len)

{

if (len<=1) return 1;

else return p[0]==p[len-1] && check1 (p+1, len-2);

}

int check (const char *p)

{

return check1 (p, strlen (p));

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

#include<stdio.h>

#include<string.h>

void main()

{

char string,string1;

printf("enter the string:\n");

scanf("%s",&string);

strcpy(string1,string);

strrev(string);

if(strcmp(string1,string)==0)

{

printf("string is palindrome");

}

else

{

printf("string is not palindrome");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

For each index from 0 to half the length of the string, if the character at the index from the left end of the string is not equal to the character at the right side of the string, the string is not a palindrome. If you escape the loop, the string is a palindrome.

This works by comparing the first character to the last character, the second character to the second-to-last character, et cetera. If the number of characters is odd, this still works; the middle character will be tested against itself, and naturally be equal to itself.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

/* using ellipses (...) to indicate tabs for clarity */

int ispalindrome (char *str) { /* determine if a string is palindromic */
... char *revstr = str; /* set to beginning of string */
... while (*revstr++ != '\0'); /* find end of string */
... revstr-=2; /* back up to the last character */
... while (str < revstr) { /* scan from both ends */

... ... if (*str++ != *revstr--) return 0; /* not palindrome */

... }

... return 1; /* palindrome */

}

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

10y ago

Yes, please do write.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a C program to check a string is a palindrome or not?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Program to check that given string is palindrome or not in C?

/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }


Write a program using method plainto check whether a string is a palindrome or not A palindrome is a string that reads the same from left to right and vice-versa?

Palindrome number is a number like 121 which remains the same when its digits are reversed. To find this number in a simple java program, just follow the below way. sum = 0; while(n&gt;0) { r=n % 10; sum=concat(r); n=n / 10; } print r;


Bluej program-read a string and check if the given string is a palindrome?

import java.util.Scanner; public class Palindrome{ public static void main(String[] args){ String front; String back =""; char[] failure; String backwards; Scanner input=new Scanner(System.in); System.out.print("Enter a word: "); front=input.next(); front=front.replaceAll(" ", ""); failure=front.toCharArray(); for (int i=0; i&lt;failure.length; i++){ back=failure[i] + back; } if (front.equals(back)){ System.out.print("That word is a palindrome"); }else System.out.print("That word is not a palindrome"); }}


Write a PHP program to check whether the number is palindrome or not?

This program only suits PHP. If you want a proper one try C program for it available on web &lt;body&gt; &lt;?php if(isset($_POST['submit'])) { $text = $_POST['text']; $string = mysql_real_escape_string($text); $invert = strrev($string); if($string == $invert) { echo "&lt;br&gt;Given number is a palindrome!!"; } else { echo "&lt;br&gt;Given number is not a palindrome!!"; } } ?&gt; &lt;form method="post"&gt; &lt;input type="text" name="text" id="text" /&gt; &lt;input type="submit" name="submit" value="Submit" id="submit" onclick="&lt;?php $_SERVER['PHP_SELF']; ?&gt;" /&gt; &lt;/form&gt; &lt;/body&gt;


Write a c program to check whether a string follows English capitalization rules?

Active Time spent online

Related questions

Write a PHP program to check whether the string is palindrome or not?

You can do this: &lt;?php if ( $word === strrev( $word ) ) { echo "The word is a palindrome"; } else { echo "The word is not a palindrome"; }


Write programs for String Reversal and Palindrome check?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.


How do you write a program in C to check whether a word is a palindrome or not?

It is a simple program. i think u may understand it :#include#include#includevoid main(){char s[10]=answers.com;char x[10];int a;clrscr();strcpy(x,s);strrev(s);a=strcmp(s,x);if(a==0){printf("the entered string is palindrome");}else{printf("the entered string is not palindrome");}output:given string is not palindrome


Program to check that given string is palindrome or not in C?

/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }


What are examples of a palindrome program forward and backwards?

To check if a string is a palindrome, point to each end of the string and work inwards towards the middle. If the characters pointed at differ, the string is not a palindrome. When the pointers meet or cross each other, the string is a palindrome. Note that the string cannot contain whitespace or punctuation and comparisons must not be case-sensitive.


Program for palindrome in php?

You could use a function like this:function isPalindrome($string) {$string = strtolower($string);return (strrev($string) == $string) ? true : false;}and then to check a palindrome call an if statement like so:if(isPalindrome($test)) {echo $test.' is a palindrome';}else {echo $test.' is not a palindrome';}


Write a program using method plainto check whether a string is a palindrome or not A palindrome is a string that reads the same from left to right and vice-versa?

Palindrome number is a number like 121 which remains the same when its digits are reversed. To find this number in a simple java program, just follow the below way. sum = 0; while(n&gt;0) { r=n % 10; sum=concat(r); n=n / 10; } print r;


Bluej program-read a string and check if the given string is a palindrome?

import java.util.Scanner; public class Palindrome{ public static void main(String[] args){ String front; String back =""; char[] failure; String backwards; Scanner input=new Scanner(System.in); System.out.print("Enter a word: "); front=input.next(); front=front.replaceAll(" ", ""); failure=front.toCharArray(); for (int i=0; i&lt;failure.length; i++){ back=failure[i] + back; } if (front.equals(back)){ System.out.print("That word is a palindrome"); }else System.out.print("That word is not a palindrome"); }}


How do you write programs for String Reversal Palindrome check?

Copy and reverse the string. If the reversed string is equal to the original string, the string is a palindrome, otherwise it is not. When working with strings that hold natural language phrases (including punctuation, whitespace and so on) we must remove all the non-alphanumerics and convert the remainder to a common case, such as lower-case, prior to copying and reversing the string.


Write a PHP program to check whether the number is palindrome or not?

This program only suits PHP. If you want a proper one try C program for it available on web &lt;body&gt; &lt;?php if(isset($_POST['submit'])) { $text = $_POST['text']; $string = mysql_real_escape_string($text); $invert = strrev($string); if($string == $invert) { echo "&lt;br&gt;Given number is a palindrome!!"; } else { echo "&lt;br&gt;Given number is not a palindrome!!"; } } ?&gt; &lt;form method="post"&gt; &lt;input type="text" name="text" id="text" /&gt; &lt;input type="submit" name="submit" value="Submit" id="submit" onclick="&lt;?php $_SERVER['PHP_SELF']; ?&gt;" /&gt; &lt;/form&gt; &lt;/body&gt;


What are the explanation steps for palindrome program?

If you want to check whether a string is a palindrome, you can reverse the string (for example, the Java class StringBuffer has a reverse() method), and then compare whether the two strings - the original string and the reverted string - are equal. Alternately, you could write a loop that checks whether the first character of the string is equal to the last one, the second is equal to the second-last one, etc.; that is, you have a counter variable (in a "for" loop) that goes from zero to length - 1 (call it "i"), and compare character #i with character #(length-i-1) inside the loop.


Can you write a C program to check whether a string is an IP address or not?

Use the Exception class and type converters.