473,382 Members | 1,424 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,382 software developers and data experts.

question with strings

I am writting a program that gets a line of input from a user and puts that in a string. Then it needs to correct whitespace and capitalization. If there is more then one space of whitespace between words change it to one space and if it is a period add a space.

this is what i have so far and Im trying to get the whitespace right.


Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7.         cout << "enter sentence and hit enter";
  8.         string  s1;
  9.  
  10.         getline(cin,s1);
  11.  
  12.          if(s1 != " ");
  13.         {
  14.  
  15.                 char seporator = ' ';
  16.                 if (s1[s1.length()-1] == '.')
  17.                 { seporator = '\n';
  18.                 }
  19.  
  20.                 cout << s1 << seporator;
  21.                 }
  22.  
  23.         while(s1 != " ");
  24.  
  25.         return(0);
  26.  
  27. }
Nov 8 '06 #1
1 1308
Banfa
9,065 Expert Mod 8TB
You have you loop wrong, you need to be looping down the string checking each character. For each character you will probably need to set some flags, was it a space?, do you need to capital ise the next character.

Then as you read each character you can use the flags to take appropriate action

Expand|Select|Wrap|Line Numbers
  1. LastCharacterWasSpace = false
  2. LastCharacterRequiresSpace = false
  3. CapitaliseNextNonSpace = true
  4.  
  5.  
  6. FOREACH Character IN String
  7.     IF Character Is Space
  8.         IF LastCharacterWasSpace = true
  9.             REMOVE Character FROM String
  10.         ELSE
  11.             LastCharacterWasSpace = true
  12.             LastCharacterRequiresSpace = false
  13.         ENDIF
  14.     ELSE // Character is not space
  15.         IF LastCharacterRequiresSpace
  16.             INSERT Space INTO String
  17.             LastCharacterRequiresSpace = false
  18.         ENDIF
  19.  
  20.         LastCharacterWasSpace = false
  21.  
  22.         IF Character = FullStop
  23.             LastCharacterRequiresSpace = true
  24.             CapitaliseNextNonSpace = true
  25.         ELSE IF Character = Comma
  26.             LastCharacterRequiresSpace = true
  27.         ELSE
  28.             IF CapitaliseNextNonSpace = true
  29.                 Character = UpperCase( Character )
  30.             ELSE
  31.                 Character = LowerCase( Character )
  32.             ENDIF
  33.             CapitaliseNextNonSpace = false
  34.         ENDIF
  35.     ENDIF
  36. ENDFOR
  37.  
Nov 8 '06 #2

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

Similar topics

3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
9
by: Rafi Kfir | last post by:
Hi, This may look as a smiple task to most of you, but to me (a beginner with C), it drives me crazy. All I want is that one function passes a two dimensional array of strings to another...
9
by: Alfonso Morra | last post by:
Hi, I am having some probs with copying memory blocks around (part of a messaging library) and I would like some confirmation to make sure that I'm going about things the right way. I have...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
3
by: M O J O | last post by:
Hi, A simple question... This is just an example ... Say I have a class (MyClass) that has only few variables (let's say 10 strings) but many-many-many subs and fuctions, so many subs and...
5
by: Simon Windsor | last post by:
Hi Is there a standard postgres method of replacing empty strings. In Oracle, nvl handles nulls and empty strings, as does ifnull() in MySQL, but with postgres coalesce only handles null...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: denis | last post by:
Hi there, I got a tough interview questions lately, and I would like to hear your opinion: An array of N chars is given Write an efficient algorithm to find all the repeating substring with a ...
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.