Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,502 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,080 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



check palidrome or not

 
Reply to this topicStart new topic

check palidrome or not

anis_ahmad
post 3 Jul, 2008 - 04:00 AM
Post #1


New D.I.C Head

*
Joined: 4 May, 2008
Posts: 7

I am trying to check string is palindrome or not but i could not get exact results what i desire.

codes are given below

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 20
void main()
{
char str[size];
int i,flag=0,j,length=0;
clrscr();
printf("input string\n");
gets(str);
for(i=0;str[i]!='\0';i++,length++)
for(i=0,j=length-1;i<=length/2;i++,j--)
if(str[i]!=str[j])
{
flag=1;
break;
}
if(flag)
printf("%s is not palindrome",str);
else
printf("%s is palindrome",str);
getch();
}

User is offlineProfile CardPM

Go to the top of the page


Mallstrop
post 3 Jul, 2008 - 04:21 AM
Post #2


New D.I.C Head

*
Joined: 19 Jun, 2008
Posts: 32



Thanked 1 times
My Contributions


CODE

for(i=0;str[i]!='\0';i++,length++)
    for(i=0,j=length-1;i<=length/2;i++,j--)
        if(str[i]!=str[j])


You have nested loops here with the first one setting i to a value and the second one over writing that value.

If you break this down into 2 tasks you will make it a much simpler problem

1. Find the length of the string.
2 with a for loop over half of length (rounded down) check that the char at i and the char at (length-i) are the same.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 3 Jul, 2008 - 05:18 AM
Post #3


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,550



Thanked 73 times

Dream Kudos: 2400

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


I just wrote this as a snippet, so I may as well give it to you...

cpp
/**
* CHECK A STRING TO FIND OUT IF IT IS A PALINDROME
* BY DANNY BATTISON
* gabehabe@hotmail.com
*/

#include <cctype> // tolower()
#include <string>

/* Check if a word is a palindrome (case sensitive!) */
bool isPalindrome (std::string word)
{
for (unsigned int i = 0; i < (word.length()/2); i++)
if (word[i] != word[(word.length()-1)-i])
return false;

/* If this point has been reached, then the word is a palindrome */
return true;
}

/* CASE INSENSITIVE */
bool isPalindromeCI (std::string word)
{
/* Convert our string, character by character */
for (unsigned int i = 0; i < word.length(); i++)
word[i] = tolower(word[i]);
/* Now our word is all lower case, we can pass it to our other function */
return isPalindrome (word);
}

/** EXAMPLE USAGE **/

#include <iostream>

int main ()
{
bool palin = isPalindromeCI ("Hannah");
/* palin is true, since we use our case insensitive version */

bool palin2 = isPalindrome ("Hannah");
/* palin2 is false, since isPalindrome is case sensitive */

std::cin.get (); /* Pause */
return EXIT_SUCCESS; /* Program was executed successfully */
}

Enjoy smile.gif
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/11/08 07:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month