473,231 Members | 1,739 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,231 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 3944
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.