#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 << " ";
}
}
}
두 번째 출력이 순서가 아니고 끝에 추가 0이있는 이유는 무엇입니까?
문자열 입력 : test
문자 입력 : t
[1] 문자가 나타나는 횟수 : 2
0 3 [2] 해당 문자는 인덱스 / 인덱스 : 0에서 찾을 수 있습니다.
main에서 값을 인쇄하는 대신 다음을 시도하십시오.
//call this from main
output_1(str, c);
output_2(str, c);
그때:
void output_1(string s, char c) {
int count = 0;
for (int x = 0; x < s.length(); x++)
if (s[x] == c)
count++;
cout<< count<<endl;
}
void output_2(string s, char c) {
for (int x = 0; x < s.length(); x++) {
if (c == s[x]){
cout << x << " ";
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다