문자열을 입력 받아 띄어쓰기를 기준으로 단어들을 배열에 저장하고 그때의 단어들의 count를 저장하는 배열을 만들어 단어와 출현 횟수를 출력하는 프로그램을 만드는 것입니다. string 배열을 써야되고 is_same_word 함수를 만들어야됩니다.
대충 제가 한 코딩을 보면 문자열을 입력받고 띄워쓰기를 기준으로 나눈뒤 무한루프를 돌립니다. 무한루프안에는 띄어쓰기가 없는경우(즉 문자열에서 단어 하나만 남는경우나 단어하나만 입력하는경우)랑 띄어쓰기가 있는 경우로 나눕니다. 띄어쓰기가 없는 경우에서 배열안에 있으면 그 배열을 찾아 count를 증가시키고 배열안에 없으면 새로운 배열에 넣어주고 count를 1로 만듭니다. 띄어쓰기가 있는 경우도 마찬가집니다. 출력되어야 될 결과 화면도 보내드립니다. ㅜㅜ 도와주세요
#include <iostream>
#include <string>
using namespace std;
int is_same_world(string arr[], string x, int y);
int main()
{
string sen; //입력할 문자열
string worlds[1000];
int counts[1000];
static int count = 0;
cout << "문장을 입력하세요. (문장의 끝은 'Enter'입니다.) \n";
getline(cin, sen, '\n'); //문자열 입력받음
int start_index = 0;
while (true)
{
int find_index = sen.find(' ', start_index); //띄워쓰기를 기준으로 find_index에 저장
if (find_index == -1) //띄워쓰기가 없는경우
{
string temp = sen.substr(start_index);
if (is_same_world(worlds, sen, count) == -1) //배열에 없는경우
{
worlds[count] = temp;
counts[count] = 1;
break;
}
else //배열에 있는 경우
{
for (int a = 0; a < count; a++)
{
if (temp == worlds[a])
counts[a] = counts[a] + 1;
break;
}
}
break;
}
else //띄워쓰기가 있는 경우
{
int temp_len = find_index - start_index; //단어의 길이
string temp = sen.substr(start_index, temp_len); //단어 복사하여 temp에 저장
if (is_same_world(worlds, sen, count) == -1) //배열에 없는경우
{
worlds[count] = temp;
counts[count] = 1;
count++;
start_index = find_index + 1;
}
else //배열에 있는경우
{
for (int b = 0; b < count; b++)
{
if (temp == worlds[b])
counts[b] = counts[b] + 1;
start_index = find_index + 1;
}
}
}
}
for (int c = 0; c <= count; c++)
{
cout << worlds[c] << " : " << counts[c] << endl;
}
return 0;
}
int is_same_world(string arr[], string x, int y) //검색하는 함수
{
int word;
for (int i = 0; i <= y; i++)
{
word = x.find(arr[y]);
}
return word;
}