space erase

closed account (4wpL6Up4)
Hi,

I am trying to write a program that erase trailing and front spaces
of a string.
The code I came up with does something weird instead,
it repeats the input several times and in some instances,
it is pushed so far to the left that part of the inputted word
disappears.
I am not sure what makes it do such a thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include "pch.h"
#include <stdlib.h>
#include<iostream>
#include<cstdlib>
#include<cctype>
#include <sstream>
#include <string>
#include<cmath>
#include <algorithm>
#include <iterator>
#include <ctype.h>
#include <iomanip>
#include <limits>

using namespace std;

int main()
{
	string name, temp;
	int i, j=0, k;
	char answer;
	do {
		cout << "enter string name" << endl;
		getline(cin, name);

		
		for (i = 0; i < name.length(); i++)
		{
			if (name[i] == ' ')
			
				
					name.erase(i, 1);
					cout << name << '\0' << endl; 
		}

		cout << "try again (Y/N)?" << endl;
		cin >> answer;
		cin.ignore();
	} while (answer == 'Y' || answer == 'y');

	return 0;
}

Last edited on
Write a function that takes the original string as an argument and returns the topped-and-tailed string.

In your function:
- find the first non-space;
- find the last non-space;
- return the substring between them.

Erasing one character at a time isn't very efficient.

You may want to specify whether "space" also includes tabs etc.


If I said you had an awful lot of "include"s ...


closed account (4wpL6Up4)
I am not sure I am following you.
All I need to know is where exactly my mistake is.
> it repeats the input several times
you\ve got a cout inside the loop, ¿what were you expecting?

> it is pushed so far to the left that part of the inputted word disappears
I don't understand, provide an example

> erase trailing and front spaces
you also kill spaces in the middle
also, you don't resolve consecutive spaces correctly


I would suggest to simply copy the characters that you want to a result string
http://www.cplusplus.com/faq/sequences/strings/trim/

1
2
3
4
5
6
7
8
9
  std::string name;
  std::cout << "What is your name? ";
  getline( std::cin, name );

  name = trim_copy( name );

// or //

  trim_inplace( name );
closed account (4wpL6Up4)
my compiler does not recognize/accept "name=trim_copy(name);"
it is underlined in red and seen as an error.

How come?
Seriously? Click the link. Copy-n-paste.
it repeats the input several times and in some instances,
it is pushed so far to the left that part of the inputted word
disappears.
You're printing the string after removing each space. Move line 33 to line 35 to print it just once when you're done processing it.

At that point you're probably find that you're removing spaces from the middle as well as the beginning and end. You will also find that you fail to remove two spaces in a row.

I am not sure I am following you.
All I need to know is where exactly my mistake is.
I think the point that lastchance was making is that your algorithm is flawed (for the reasons I mention above). He's showing you an algorithm that will work.

The code that Duthomhas links to will work, but if this is an assignment, you should try to write it yourself instead using lastchance's suggestion or ne555's.
not the most efficient, but its easy to code and might help you get started. Sorry, still learning how some of the string stuff works myself..

1
2
3
4
5
6
7
8
9
10
11
int main()
{	
string t = "tesx   ";
int s = 0;
int e = t.length()-1;
while(t[s] == ' ') s++;
while(t[e] == ' ') e--;
t  = t.substr(s,e-s+1);
cout <<t << endl;
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
//std::string line;
{ //trim at the start
   std::istringstream aux(line);
   std::getline(aux >> std::ws, line);
}
{ //trim at the end
   std::reverse(line.begin(), line.end());
   std::istringstream aux(line);
   std::getline(aux >> std::ws, line);
   line = std::string(line.rbegin(), line.rend());
}
this might help you:

#include <iostream>
using namespace std;
int main()
{
string str;
cout<<"Enter the string ";
getline(cin,str);
int len=str.length();
char newstr[len];

//Removing one or more blank spaces from string
int i=0,j=0;
while(str[i]!='\0')
{
while(str[i] == ' ')//using loop to remove consecutive blanks
i++;
newstr[j]=str[i]; //newstr[j++]=str[i++] we can also use this instead
i++;
j++;
}
newstr[len-1]='\0';//terminating newstr, we use -1, as j was a post increment.
cout<<"\n String after removal of blank spaces is:"<<newstr;
return 0;
}



source: https://www.studymite.com/cpp/examples/removing-the-blank-spaces-from-string-in-cpp/
Topic archived. No new replies allowed.