Question Regarding Strings

I am trying to make every repeated character in my sub_string to a '0'. But it is not going through the second loop that is supposed to set the index of the repeated character to 0 (as it is written on line 46). Any suggestions on how to fix this?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <vector>
using namespace std; 

/*Write a program that allows a user to type in tabular data similar to a CSV file, but instead of using commas a 
separtor, you should try to detect valid separators. First, let the user type in the line of tabular data. Then 
detect possible separator characters by looking through the input for non-number, non-letter, non-space characters. 
Find all of these chracters that appear on every single line, and display the user these characters to ask which 
one to use. For example, if you see input like this: Alex Allain, webmaster@cprogramming.com John Smith, 
john@nowhere.com. You should prompt the user to choose between comma, at sign, and period for the separtor. */

int main ()
{	
	vector<int>symbol_index (10); 
	string symbol_subset;  
	string symbol_set = "~`!@#$%^&*()-_=+{[}]|:;?/>.<,'\\\"";
	string input = "jae.kim@jhykima, I like icecream & water, #coding."; 
	int k = 0; 
	for (int j = 0; j < symbol_set.length (); j++)
	{
		for (int i = input.find (symbol_set[j], 0); i != string::npos; i = input.find (symbol_set[j], i))
		{	
			if (k > 9)
			{
				symbol_index.push_back (i); 
			}
			else
			{
				symbol_index[k] = i;
			}
			k++;  
			i++; 
		}
	}
	for (int i = 0; i < k; i++)
	{
		symbol_subset[i] = input[symbol_index[i]];
	}

	for (int i = 0; i < k; i++)
	{
		cout << symbol_subset[i] << endl;
	}

	for (int j = 0; j < symbol_set.length (); j++)
	{
		int t = 0; 
		for (int i = symbol_subset.find (symbol_set[j], 0); i != string::npos; i = symbol_subset.find (symbol_set[j], i))
		{
			if (t == 0)
			{
				t = -1; 
			}
			else
			{
				symbol_subset[i] = '0';
			}
			cout << i << endl;
			i++;
		}
	}

	for (int i = 0; i < k; i++)
	{
		cout << symbol_subset[i] << endl;
	}
}

Last edited on
The std::string.find() function is searching a string for a string. You probably want to search your string for a character instead. You may want to try the std::string.find_first_of() function instead.

Also remember that the find() family of functions can return values that can't be represented by an int value. You really should be using size_t instead of the int, because comparing string::npos to an int value will probably not produce the desired results.

Topic archived. No new replies allowed.