473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reading string without spaces and capital letters

11
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main ()
  5.  
  6. {
  7.     char s[100];
  8.     int i,j;
  9.     bool match=true;
  10.     cin.getline(s,100,'\n');
  11.     cout<<endl;
  12.     i=0;
  13.     j=strlen(s)-1;//because of the \0
  14.  
  15.  
  16.  
  17.     while(match&&(i<j))
  18.     {
  19.         if(s[i]!=s[j])
  20.             match=false;
  21.         i++;
  22.         j--;
  23.     }
  24.     if (match)
  25.         cout<<"It is a palindrome"<<endl;
  26.     else
  27.         cout<<"It is not a palindrome"<<endl;
  28.  
  29.     return 0;
  30. }
can anyone help? thank you.
Dec 2 '07 #1
2 3970
Never rely to much on C getline if you have to do line-level character computations;use instead a getchar:getchar fetches a byte from the standard input;you should embed it in a while loop
Expand|Select|Wrap|Line Numbers
  1. while (c= getchar() != \n)
  2.  do your computations
  3.  
is that clear enough?I hope so
have a good sunday!
Dec 2 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You need to ignore spaces in the compare inside your loop.

That is, if s[i] is a space you need a loop so skip all spaces until the next character. Ditto for s[j]. Maybe you write a a function to call here. It can take the input sttring and current position as arguments plus a third argument to say whether to go forwards are backwards in the string. It could return the position of the next non-space character, or -1 if you run out of string. Then you can call this function rather than using ++i and ++j in your loop.

BTW, your cin.getline is just fine. getchar() is a C carry over to C++ and is not required in this case.
Dec 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff | last post by:
Hi, I am trying to insert a space before all capital letters in a string except the first occurrence. For example: FirstSecondThird would become First Second Third Is there a regular...
3
by: - ions | last post by:
Hi, i would like to know how to replace every char in a string with a certin given char using the String.replace(char oldChar,char newChar). I would like to replace all letters with an underscore...
4
by: Carlos Marangon | last post by:
Hello! People go to sign my guestbook and wrote all text in capital letters. Did you know any script that shows an window alert when one types the second capital letter? Best regards,
15
by: Randall Parker | last post by:
I've noticed when exporting from Microsoft Word XP into an HTML file that Word uses a span style of mso-spacerun: yes. This has the effect of making there be about 2 spaces between sentences. So...
6
by: Gidi | last post by:
Hi, I'm writing a C# Windows Application and i have 3 questions: 1. I have a string which i want to write into file but i want to write it in ASCII format, how can i do it? 2. I need my...
22
by: hg | last post by:
Hi, I'm bringing over a thread that's going on on f.c.l.python. The point was to get rid of french accents from words. We noticed that len('à') != len('a') and I found the hack below to fix...
12
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
4
by: jerger | last post by:
i have a great program now with the help of a member from this site, but i need a little customization to meet the needs of non-english speakers... who might accidendtly type punctuation which would...
6
by: 182719 | last post by:
<?php $testcase = 'AKLWC139'; if (ctype_upper($testcase)) { echo "The string $testcase consists of all uppercase letters. \n"; } else { echo "The string $testcase does not consist of all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.