Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,224 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,261 people online right now. Registration is fast and FREE... Join Now!




reading string without spaces and capital letters

 
Reply to this topicStart new topic

reading string without spaces and capital letters

inferi
1 Dec, 2007 - 11:26 PM
Post #1

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 7


My Contributions
hi,
I am writng a program that takes a string and it must output if it is a palindrome,
A palindrome is a string that is spelled and read the same way forward and backward, it workes fine with one word but if there is spaces or capital lettersor commas it won't work I used getline but without any effect.here is the code.


CODE
Code: ( text )
#include <iostream>
#include <string>
using namespace std;
int main ()

{
    char s[100];
    int i,j;
    bool match=true;
    cin.getline(s,100,'\n');
    cout<<endl;
    i=0;
    j=strlen(s)-1;//because of the \0
    
    
    
    while(match&&(i<j))
    {
        if(s[i]!=s[j])
            match=false;
        i++;
        j--;
    }
    if (match)
        cout<<"It is a palindrome"<<endl;
    else
        cout<<"It is not a palindrome"<<endl;
    
    return 0;
}



can anyone help? thank you.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Reading String Without Spaces And Capital Letters
2 Dec, 2007 - 12:06 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You might want to check out this earlier discussion that may help you with this problem. The idea is to use recursion to compare the first character with the last character, subtract 1 and call it again. It should work with spaces, punctuation and even case sensitive.

Detecting a palidrome thread - Sep 5th

Hope it pushes you in the right direction. smile.gif
User is offlineProfile CardPM
+Quote Post

inferi
RE: Reading String Without Spaces And Capital Letters
2 Dec, 2007 - 03:19 AM
Post #3

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 7


My Contributions
Thank you soooooo much you were a great help
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:59AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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