7.06.2017

가장 긴 공통 접두어

//C++로 시작하는 객체지향 프로그래밍 p.334 예제 7.32 - 가장 긴 공통 접두어
#include <iostream>
#include <string>
using namespace std;

void prefix(const char s1[], const char s2[], char commonPrefix[]){
int len = strlen(s1);
int i = 0;
for (i = 0; i < len; i++){
if (s1[i]==s2[i]) {
commonPrefix[i] = s1[i];
}else break;
}
commonPrefix[i] = '\0'; // Set a null terminator
}
int main(){
//Prompt the use to enter two strings
cout << "Enter a string s1: ";
char s1[80];
cin.getline(s1, 80);

//Prompt the user to enter two strings
cout << "Enter a string s2: ";
char s2[80];
cin.getline(s2, 80);
char s3[80];

prefix(s1,s2,s3);

if(strlen(s3) ==0)
cout << "No common prefix" << endl;
else cout << "The commom prefix is " << s3 << endl;
system("pause");
return 0;
}

댓글 없음:

댓글 쓰기