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

Join 136,477 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,594 people online right now. Registration is fast and FREE... Join Now!




check palidrome or not

 
Reply to this topicStart new topic

check palidrome or not

anis_ahmad
3 Jul, 2008 - 03: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
+Quote Post

Mallstrop
RE: Check Palidrome Or Not
3 Jul, 2008 - 03: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
+Quote Post

gabehabe
RE: Check Palidrome Or Not
3 Jul, 2008 - 04:18 AM
Post #3

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

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 offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:14PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month