'tokenize'에 해당되는 글 1건
- 2009.08.17 strtok()를 이용한 문자열 단어 교체 프로그램
strtok()를 이용한 문자열 단어 교체 프로그램
2009. 8. 17. 17:17 in 공부합시다/C언어

문자열은 30자로 제한
교체할 문자는 10자로 제한한다고 가정
실행화면
교체할 문자는 10자로 제한한다고 가정
- #include <stdio.h>
- #include <string.h>
- char * change_word(char *string, char *old_word, char *new_word);
- int main()
- {
- char string[30];
- char old_word[10];
- char new_word[10];
- printf("Input string: ");
- fgets(string, sizeof(string), stdin);
- printf("Input old word: ");
- scanf("%s", old_word);
- printf("Input new word: ");
- scanf("%s", new_word);
- strcpy(string, change_word(string, old_word, new_word));
- printf("change string: %s\n", string);
- return 0;
- }
- char * change_word(char *string, char *old_word, char *new_word)
- {
- char *token;
- char temp[30];
- memset(temp, 0, sizeof(temp));
- token = strtok(string, " ");
- while(token != NULL)
- {
- if(0 == strcmp(token, old_word))
- {
- strcat(temp, new_word);
- }
- else
- {
- strcat(temp, token);
- }
- strcat(temp, " ");
- token = strtok(NULL, " ");
- }
- temp[strlen(temp)-1] = 0;
- return temp;
- }
실행화면
'공부합시다 > C언어' 카테고리의 다른 글
[C언어] 부호 확장(Sign extension) (0) | 2009.05.06 |
---|---|
[C언어] 구조체의 메모리 저장방식과 #pragma pack (0) | 2009.04.30 |
[C언어]좀 더 복잡한 함수 포인터 (0) | 2009.04.20 |
[C언어]Little Endian (0) | 2009.04.16 |
[C언어]함수 주소를 직접 입력하여 호출하기 (0) | 2009.04.16 |