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.
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
usingnamespace 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;
}
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++;
}
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.
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
usingnamespace 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 ','.
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.
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.
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
usingnamespace 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;
}
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
usingnamespace 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.
#include <iomanip>
#include <cmath>
#include <iostream>
#include <string>
usingnamespace 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');
}