Using string function help?

I need to write a program that will let the user input a string and a character to search for. It should output the number of times that character is in the string. I am just having trouble as to how i am supposed to add up the number of times each one is found. Also, i am supposed to start the index at 0 and then change it to 1 past the index of where the character was last found. i know i am (very) new to c++, so any help would be appreciated.

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

main()
{ string str1;
	char search;
	char i;
	int count = 0;

	
	cout<<"This program will let the user input a string and then find a character in that string"<<endl;
	cout<<"Please input a string"<<endl<<endl;

	getline(cin, str1);
		cout<<"The string you entered was "<<str1<<endl;
	cout<<"Please input a character to count"<<endl<<endl;
	cin>>search;

	for(i=0, i<int(str1.length()), i++)
	{ str1.find(search,(0));



	}
	cout<<"There are "<<count<<search<<"'s in the string"<<endl;

}
Last edited on
Typically you would declare the looping variable in the for loop.


The variable i should really be an integer and not a char.

As a beginner you shouldn't start with the std::string functions (such as find). In this case simply increase 'i' in the for loop and check the identity and then increase the count if the identity matches true.

1
2
3
4
5
6
7
8
9
10

for (int i = 0 ; i < str1.length();i++)
{
           if (str1[i] == search)
                 count++;



}
Last edited on
well i am in a programming class and we are supposed to use string functions (specifically find). But yes, that way is simpler but i am not supposed to do it that way.
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
 #include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

main()
{ string str1;
	char search;
	char i;
	int count = 0;


	cout<<"This program will let the user input a string and then find a character in that string"<<endl;
	cout<<"Please input a string"<<endl<<endl;

	getline(cin, str1);
		cout<<"The string you entered was "<<str1<<endl;
	cout<<"Please input a character to count"<<endl<<endl;
	cin>>search;

	for(i=0; i<str1.length(); i++)
	{
        int pos_found = str1.find (search,i);

        if (pos_found != string::npos)
        {
            count++; // new char found
            i = pos_found; //start at that character
        }


	}
	cout<<"There are "<<count<<search<<"'s in the string"<<endl;

}


Should do the trick. If you need help understand it then ask.

You should try to look at some simple examples. The for loop components should be separated by a ';' not a ','.
Last edited on
i have been looking at my book and examples, just wasn't getting it. I think i wasn't setting the equation equal to anything, so i wasn't getting an output. Why does your find(string) equation in line 24 have an i, whilst mine has a 0? Does it make a difference since i is set to start at 0?
The second parameter to the find function is where the search begins at. This should be i because each time you find a new character then you want to skip to that character + 1 (as you have stated) as opposed to searching from 0 again. If you had 0 in my loop then the program would get stuck in a loop because it would never get past the first matched character.
Also, what is 'string::npos', i haven't learned that yet.
http://www.cplusplus.com/reference/string/string/find/

Look at the return value:

If there is a match then the pos of the match.

If no match then a special number (string::npos).
ok, thanks for explaining that. ill look at that link. Is there a way to do it without using that, though? just wondering.
Not really. You need to know where there is a match or not. As I said using std::find is overkill for this.
Yea, i agree that it is overkill too. So i tried to compile it, and when i enter a character for 'search' (i changed to char1 in this actually) and press enter, nothing happens.

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
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>

using namespace std;

int main()
{   string str1;
	char char1;
	int i;
	int charcount = 0;
	char pos_found;
	
	cout<<"This program will let the user input a string and then find a character in that string"<<endl;
	cout<<"Please input a string"<<endl<<endl;

	getline(cin, str1);
		cout<<"The string you entered was "<<str1<<endl;
	cout<<"Please input a character to count"<<endl<<endl;
	cin>>char1;

	for(i=0; i<int(str1.length()); i++)
	{ int pos_found = str1.find(char1,(i));

	if (pos_found != string::npos)
		
		charcount++;

	i = pos_found;

	

	}

	
	cout<<"There are "<<charcount<<char1<<"'s in the string"<<endl;
	

	system("pause");
return 0;
}
I'm not sure why you keep changing the code:

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

main()
{ string str1;
	char search;
	char i;
	int count = 0;


	cout<<"This program will let the user input a string and then find a character in that string"<<endl;
	cout<<"Please input a string"<<endl<<endl;

	getline(cin, str1);
		cout<<"The string you entered was "<<str1<<endl;
	cout<<"Please input a character to count"<<endl<<endl;
	cin>>search;

	for(i=0; i<str1.length(); i++)
	{
        int pos_found = str1.find (search,i);

        if (pos_found != string::npos)
        {
            count++; // new char found
            i = pos_found;
        }


	}
	cout<<"There are "<<count<<search<<"'s in the string"<<endl;

}
I had the system pause in there because im going to put a do-while loop in there. It works the first time through after the initial time, but if i try to do it a second run through it exits 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>

using namespace std;

int main()
{   string str1;
	char char1;
	int i;
	int charcount = 0;
	char pos_found;
	char y = 0;
	char answer;
	
	cout<<"This program will let the user input a string and then find a character in that string"<<endl;
	
	do
	{
	cout<<"Please input a string"<<endl<<endl;

	getline(cin, str1);
		cout<<"The string you entered was "<<str1<<endl;
	cout<<"Please input a character to count"<<endl<<endl;
	cin>>char1;

	for(i=0; i<str1.length(); i++)
	{ int pos_found = str1.find(char1,i);

	if (pos_found != string::npos)
		
		charcount++;

	i = pos_found;

	

	}

	
	cout<<"There are "<<charcount <<char1<<"'s in the string"<<endl;
	cout<<"Do you wish to run this again? y or Y for yes, any other button for no"<<endl;
	cin>>answer;
	}
	while(answer == 'y' || answer == 'Y');
	

}
Topic archived. No new replies allowed.