String manipulation code

Hey guys, I'm trying to take text from a file, save it into a string, then isolate letters only and save the word itself.

example:

input --> "hey,"
saves --> "hey"

OR.

input --> "(Bob)"
saves --> "Bob"

This is what I have...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int length = Unchecked_Word.length();
				for(int i = 0; i < length; i++)
				{
					//subtracts sentence structure points
					if(Unchecked_Word.at(i) == '(' || Unchecked_Word.at(i) == ')' || Unchecked_Word.at(i) == ',' || Unchecked_Word.at(i) == '.' || 
					   Unchecked_Word.at(i) == '"' || Unchecked_Word.at(i) == '-' || Unchecked_Word.at(i) == ' ' || Unchecked_Word.at(i) == '?')
					{
						Unchecked_Word.at(i) = ' ';
					}
					//Converts first letter in Unchecked_Word to lowercase
					else if(Unchecked_Word.at(i) <= 90  && Unchecked_Word.at(i) >= 65)
					{
						Unchecked_Word.at(i) = Unchecked_Word.at(i) + 32;
					}
				}


The problem with this is that when you get input like "(Bob)", then it saves " Bob ", rather than the preferred "Bob" with no spaces attached. Any help is much appreciated!
Your code is replacing any of the target characters with ' ', you want it to erase the character:
http://www.cplusplus.com/reference/string/string/erase/

Unchecked_Word.erase(i, 1);

If you are allowed to use iterators, I would recommend them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>

int main(void)
{
  std::string str("hello world");
  std::string::iterator str_iter;
  
  for (str_iter = str.begin(); str_iter != str.end(); str_iter++)
  {
    if (*str_iter == 'o')
      str.erase(str_iter);    
  }
  
  std::cout << str << '\n';
  return 0;
}
It seems you are attempting to remove any punctuation from an std::basic_string object. I'm only able to think of two options. You can choose to erase a character from the object using the std::basic_string::erase() member function. Or you could choose to append the new lowercase character to another std::basic_string object.

http://en.cppreference.com/w/cpp/string/basic_string/erase
http://en.cppreference.com/w/cpp/string/basic_string/operator%2B%3D

Below is a link showing how I chose to isolate the word.
http://ideone.com/CpZeqg
Last edited on
@LowestOne: you should not iterate and delete a character at the same time


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>

int main(void)
{
  std::string str("good");
  std::string::iterator str_iter;
  
  for (str_iter = str.begin(); str_iter != str.end(); str_iter++)
  {
    if (*str_iter == 'o')
      str.erase(str_iter);    
  }
  
  std::cout << str << '\n';  //print 'god'
  return 0;
}


perhaps copy valid characters from old string to a new string is better, or use stringstream...
Last edited on
How would I implement a string stream?
I've reworked it, now I get a fatal error when I debug the program.

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
#include <iostream>
#include <string>
using namespace std;

int main() {

	string Unchecked_Word("(hi)");
	int length = Unchecked_Word.length();

	for(int i = 0; i < length; i++)
	{
		//subtracts sentence structure points
		if(i <= length)
		{
			if (Unchecked_Word.at(i) == '(' || Unchecked_Word.at(i) == ')' || Unchecked_Word.at(i) == ',' || Unchecked_Word.at(i) == '.' || 
			   Unchecked_Word.at(i) == '"' || Unchecked_Word.at(i) == '-' || Unchecked_Word.at(i) == ' ' || Unchecked_Word.at(i) == '?')
			{
				Unchecked_Word.erase(i,1);
			}
		}

		//Converts first letter in Unchecked_Word to lowercase
		else if(Unchecked_Word.at(i) <= 90  && Unchecked_Word.at(i) >= 65)
		{
			Unchecked_Word.at(i) = Unchecked_Word.at(i) + 32;
		}
	}

	cout << Unchecked_Word << endl;

	cin.ignore(); cin.get(); return 0;
}


Now what have I done...
@tntxtnt: You're correct, I remade it:
1
2
3
4
5
for (std::string::iterator str_iter = str.begin(); str_iter != str.end(); str_iter++)
{
  if (*str_iter == 'o')
    str.erase(str_iter--);
}


@jjwarns: When you erase from the string, you need to decrease length. You also have the same issue I had.

You can also use find_first_of():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>

int main(void)
{
  std::string toFind("(){}[]");
  std::string str("(this) is {the }test {of ]this][ pro(gram");
  size_t str_pos = 0;
  while ((str_pos = str.find_first_of(toFind, str_pos)) != std::string::npos)
	str.erase(str_pos,1 );
  
  std::cout << str << '\n';
  return 0;
}
Last edited on
Topic archived. No new replies allowed.