Monday, May 24, 2010

C++ program to check whether the string is palindromic or not?

please check the code for a question "Write a program to check whether the given string is palindromic or not."


#include %26lt;iostream.h%26gt;


#include %26lt;conio.h%26gt;





void main


{ char x[100];


char y[100];


int temp1,temp2;


int ans = 0;


cout %26lt;%26lt; "Enter the word ";


cin %26gt;%26gt; x;


cout %26lt;%26lt; "Enter the word ";


cin %26gt;%26gt; y;


temp1 = strlen(x);


temp2 = strlen(y);


for(int k = 0;k %26lt; temp1 ;k--)


{for(int j = temp2;j %26lt;= ......;j--)


{if(x[k] == j[k])


ans = 1;


continue;


else


ans = 0;


}}


if(ans == 1)


cout %26lt;%26lt; "The string is palindromic";


else


cout %26lt;%26lt; "The string is not palindromic";


getch();


}


please make the necessary corretions as i dont have a compiler please help!!!!!!!!

C++ program to check whether the string is palindromic or not?
#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





int main()


{


char str[100];





cout %26lt;%26lt; "Enter word :";


cin %26gt;%26gt; str;


int x = strlen(str)-1;


for(int i = 0; i %26lt;= x; i++)


{


if (str[i] == str[x-i])


{


continue;


}


else


{


cout%26lt;%26lt;"Not a palidrome"%26lt;%26lt;endl;


return 0;


}


}





cout %26lt;%26lt; "Indeed Palidrome"%26lt;%26lt;endl;


return 0;


}
Reply:Your code is messed up and I don't really understand your logic, if you want to chec whether a string is palindromic, you need only enter one string, not 2.





if it helps, this is how I'd do it, although it is written in C, you should not have much trouble understanding it, specially if you use google.








#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


#include %26lt;ctype.h%26gt;





void trimSpaces(char *buffer);





int main()


{


char buffer[1024];


int start_ctr, end_ctr;


int is_pal;





fgets(buffer, 1023, stdin);


trimSpaces(buffer);





printf("After trim: %s\n", buffer);





end_ctr = strlen(buffer) - 1;


start_ctr = 0;


is_pal = 1;





while( (start_ctr != end_ctr) %26amp;%26amp; (end_ctr %26gt; start_ctr) )


{


if( tolower(buffer[start_ctr]) != tolower(buffer[end_ctr]) )


{


is_pal = 0;


break;


}


start_ctr++;


end_ctr--;


}





if( is_pal )


{


printf("Palindromic, indeed :)\n");


}


else


{


printf("Not palindromic :(\n");


}





return 0;


}





void trimSpaces(char *buffer)


{


int i = 0, j = 0;





for(; buffer[i] != '\0'; i++ )


{


if( isalpha(buffer[i]) )


{


buffer[j] = buffer[i];


j++;


}


}





buffer[j] = '\0';


}








Anobody besides me thinks yahoo needs a CODE tag?
Reply:Hi I study in DPS Ahmedabad if you know me please contact me(i will give you the compiler). This Pogramme will definately work for palindrome check


#include %26lt;iostream.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;string.h%26gt;


void main()


{


clrscr();


char x[100];


char y[100];


int temp1,temp2=0;


cout %26lt;%26lt; "Enter the word ";


cin %26gt;%26gt; x;


temp1 = strlen(x)-1;


for(int j= 0;j%26lt;strlen(x);j++)


{


y[j] = x[temp1 - j];


}


y[j] = '\0';


x[j] = '\0';


for(int k =0; k%26lt;strlen(x); k++)


{


if (x[k]==y[k]){


temp2 = temp2 + 1;


}


}


if (strlen(x) == temp2)


{cout%26lt;%26lt;"Palindrome";


}


else


{cout%26lt;%26lt;"not a palindrome";


}


getch();


}

blazing star

No comments:

Post a Comment