473,786 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading string without spaces and capital letters

11 New Member
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 4000
MarshMallow
52 New Member
Never rely to much on C getline if you have to do line-level character computations;us e 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 Recognized Expert Moderator Expert
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
11006
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 expression or short function that can do this?
3
17815
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 ie. "hello world" will be come... "_ _ _ _ _ _ _ _ _ _" (ive added xtra spaces so it dosnt look like one line!) with the method i have written only the last char is replaced, loop problem i guess! public String hideWord(String word) {
4
10772
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
29800
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 you will see a sentence and then the tag.<span style='mso-spacerun: yes'> </span>Well, this next sentence will occur two spaces later because there are two spaces in the span. How to do this without using MS's style? Is there some way to...
6
2028
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 string to be 120 chars length, incase it's less then 120 chars, i need to fill it with spaces, is there better way to do it then:
22
2623
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 the "problem" ... yet I do not understand - especially since 'ŕ' is included in the extended ASCII table, and thus can be stored in one byte.
12
4391
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
4793
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 give a false negative dictionary response... so if the user types in: i need uppercase to become lowercase too what i would like is to remove commas, periods, exclamation points and question marks and apostrophes from the strings before it...
6
5679
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 uppercase letters.\n";
0
10357
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.