can u tell me a way to remove the excess spaces from a sentance using Ws? or anyother way?

Lets say i have "This is a sentance"
I want "This is a sentance"

with ws?

Recommended Answers

All 10 Replies

Both lines are identical, what do you want?

Well i wanted to remove the extra spaces but,it didnt come out in the post properly
You know in html,the extra spaces are automatically removed,thats how

Lets say i have "This         is            a           sentance"
I want "This is a sentance"

can i do it using ws manipulator or any other simple way?

TrintiyNoe,

Did you work out on this problem? If yes; then show us your code, please.
However I suggest:

string str="This   is     a   sentance";
istringstream iss(str);
string str = "This    is a     sentence.";
string newStr;
int len = 0;
for(int i = 0; i < str.len; i++)
{
    newStr += str[i];
    if(str[i] == ' ')
        while(str[++i] == ' ')
            ;
}

I made this in 2 mins and didn't test it. But you get the basic idea..

Actually,i encounter this in lot of probs,i use while loop to solve it,not unlike the above version,but i thought it would be easier if there is one simple way doin like a function or somethin.

Well you can always write your own function and add it to the headers and include it in all your usages. Its quite common . If its too good then it may be included in the standard headers in future ;).

Well you can always write your own function and add it to the headers and include it in all your usages. Its quite common . If its too good then it may be included in the standard headers in future ;).

;) Hope so

but i thought it would be easier if there is one simple way doin like a function or somethin.

You can write your own function to do it. :) But doing it in a general way is not as easy as you thing. Removing all spaces is easy but leaving one space isn't because a space character could be more than just a single space. If you have "a\t\tb", is it right to replace it with "a b"? What should "a\t\t\nb" be replaced with?

Basically what i want to do is,extract the first letters of words in a string and put it in a vector.

If i have "This     is                   the           world"

I want a vector to collect only T, i, t ,w

so it would have been easier for me to just trim the string if u will and take all the chars whose previous charactor is ' '.

Did the problem now,using the old fashioned for way
but,would like that function to be added to standard library.Maybe one of u guys can do it :P

That's easy with the standard library:

istringstream iss(s);
vector<char> v;
string word;

while (iss >> word) v.push_back(word[0]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.