strtok 이용해서 .이나 ,으로 나뉘어진 문자열 단위로 토큰을 만들고 각각 처리
이때 .나 , 뒤로 공백문자가 있는 경우만을 포함하도록 유의하여 처리해야함
strtok를 사용하면 기존 문자열 버퍼의 구분자 위치를 NULL 문자가 대체하게 되므로 예외 상황일 때 지워진 문자를 복구하는 것까지 고려해야 할듯
직전 토큰 포인터를 pp, 현재 토큰 포인터를 p라고 했을때
strtok 반복하면서 p값 얻고 p+1 위치에 있는 문자가 공백인지 아닌지 확인
-> 공백이면 문장이 나뉜거니까 pp~p-2까지 처리하고 pp 업데이트
-> 공백이 아니면 문장이 나뉜게 아니니까 pp를 업데이트하지 않고 p-1 위치의 값 복구 & p부터 strtok 재개
대강 이런 방식으로 생각해보셈 세부적인 건 잘못된 부분이 있을수있음
일단 too를 to로 바꾸는 코드로 #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SIZE 1000
int main(void) {
char str[SIZE] = "A new batch of inflation data released Friday showed that while prices remained uncomfortably high in September, a slowdown in wage growth indicates some relief may be in sight. That's an encouraging development for the Federal Reserve, which is battling too bring down the highest inflation in 40 years. The Personal Consumption Expenditures Index, which measures prices paid by consumers for goods and services, climbed by 0.3% from August too September but remained unchanged at 6.2% for the year, according too the latest report from the Bureau of Economic Analysis.";
char find[SIZE] = "too";
char replace[SIZE] = "to";
char* result[SIZE] = { 0 };
char* ptr;
int i, cnt = 0;
ptr = strtok(str, " ");
for (i = 0; ptr != NULL; i++) {
result[i] = ptr;
ptr = strtok(NULL, " ");
cnt++;
}
for (i = 0; i < cnt; i++) {
if (strcmp(find, result[i]) == 0) {
result[i] = replace;
}
}
printf("결과: ");
for (i = 0; i < cnt; i++) {
printf("%s ", result[i]);
}
} 대충 이런 식으로 만들었는데 솔직히 그 코드를 어떻게 붙여쓸지 잘 모르겠습니다 ㅠ