80개 이하의 문자로 이루어진 문자열 a과 또다른 문자열 b를 입력받아 a에 b의 문자열이 포함되어있으면 그 위치를 출력하고 그렇지 않으면 -1을 출력한다.
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
string a,b;
int i,j;
bool accord,find;
getline(cin,a);
getline(cin,b);
find = false;
for(i=0;i<=a.length()-b.length();i++){
accord = true;
for(j=0;j<b.length();j++){
if(a[i+j]!=b[j]){
accord=false;
break;
}
}
if(accord){
cout<<i+1<<endl;
accord=true;
find = true;
}
}
if(!find)cout<<-1;
system("PAUSE");
return 0;
}
다음은 실행결과다.
all right, i was willing to go make a try for a fat doe. I was generally mare than willing to go hunting.
was
14
60
Press any key to continue . . .
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
string a,b;
int i,j;
bool accord,find;
getline(cin,a);
getline(cin,b);
find = false;
for(i=0;i<=a.length()-b.length();i++){
accord = true;
for(j=0;j<b.length();j++){
if(a[i+j]!=b[j]){
accord=false;
break;
}
}
if(accord){
cout<<i+1<<endl;
accord=true;
find = true;
}
}
if(!find)cout<<-1;
system("PAUSE");
return 0;
}
다음은 실행결과다.
all right, i was willing to go make a try for a fat doe. I was generally mare than willing to go hunting.
was
14
60
Press any key to continue . . .
댓글
댓글 쓰기