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

Question about strings

Im writing a progrem that takes input (a sentence) from the user and I about it in the proper format. All i need now is to figure out how to get the spacing correct in the sentence. I need all the strings of two or more blank spaces should be compressed to one single space. Here is what I got so far. Any ideas on how to go about doing the spacing would be greatly appreciated. Thanks!!

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str;
cout << "Enter your sentence and then press enter. " << endl;
getline (cin, str);
str[0] = toupper (str[0]);
for (int i = 1; i < str.length(); i++)
{
str[i] = tolower (str[i]);
}
cout << str << endl;

}
Nov 12 '06 #1
3 1232
Banfa
9,065 Expert Mod 8TB
You can do this in 1 variable but I think it is easier (and requires less copying of data to do in in 2.

Expand|Select|Wrap|Line Numbers
  1. Initialise result string to ""
  2. Get input string
  3.  
  4. For each character in the input string
  5.     If it is not space or the previous character was not space
  6.         Copy character to the result string
  7.     End If
  8. End For
  9.  
Nov 13 '06 #2
You can do this in 1 variable but I think it is easier (and requires less copying of data to do in in 2.

Expand|Select|Wrap|Line Numbers
  1. Initialise result string to ""
  2. Get input string
  3.  
  4. For each character in the input string
  5.     If it is not space or the previous character was not space
  6.         Copy character to the result string
  7.     End If
  8. End For
  9.  
ALright i tried to do what you said but I'm a begginner programmer and Im not sure how to do some of the things you said: here's what i got now:

string result;
string str;
cout << "Enter your sentence and then press enter. " << endl;
getline (cin, str);
str[0] = toupper (str[0]);
for (int i = 1; i < str.length(); i++)
{
str[i] = tolower (str[i]);
}
result = "";
i = 0;
for (int i = 0; i < str.length(); i++)
if (str[i] != " " || str[i-1] != " ")
return = result;

cout << str << endl;

Im getting errors witht hat...I don't know how to copy character to result string or enter in if not a space i just guessed to use " "....Please help
Nov 13 '06 #3
horace1
1,510 Expert 1GB
you have a couple of errors indicated by // ** comments
Expand|Select|Wrap|Line Numbers
  1.     string result;
  2. string str;
  3. cout << "Enter your sentence and then press enter. " << endl;
  4. getline (cin, str);
  5. str[0] = toupper (str[0]);
  6. for (int i = 1; i < str.length(); i++)
  7. {
  8. str[i] = tolower (str[i]);
  9. }
  10. result = "";
  11. //i = 0;                               // ** removed
  12. for (int i = 0; i < str.length(); i++)
  13.    if (str[i] != ' ' || str[i-1] != ' ') // ** replaced " with '
  14.      return = result;
  15. cout << str << endl;
  16.  
you now need to replace the line
Expand|Select|Wrap|Line Numbers
  1. return = result;
  2.  
with code which adds the character from str[i] to result
Nov 13 '06 #4

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: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.