#include <iostream>
using namespace std;
int output_1(string s, char c);
int output_2(string s, char c);
int main() {
string str;
char c;
cout << "Enter a string: ";
getline(cin, str);
cout << "Enter a character: ";
cin >> c;
cout << "[1] the number of times the character appears: " << output_1(str, c) << endl;
cout << "[2] That character is found at Index/Indices: " << output_2(str, c) << endl;
return 0;
}
int output_1(string s, char c) {
int count = 0;
for (int x = 0; x < s.length(); x++)
if (s[x] == c)
count++;
return count;
}
int output_2(string s, char c) {
for (int x = 0; x < s.length(); x++) {
if (c == s[x]){
cout << x << " ";
}
}
}
Why is the second output not in order and have an extra 0 at the end?
Enter a string: test
Enter a character: t
[1] the number of times the character appears: 2
0 3 [2] That character is found at Index/Indices: 0