"C언어 단어 추출"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 6개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
<source lang='c'>
<syntaxhighlight lang='c' run>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
10번째 줄: 10번째 줄:
     char *words[NUMBER_OF_STRINGS];
     char *words[NUMBER_OF_STRINGS];
     char seps[] = "[]'`_*?0123456789()\",.! -:;/\t\n";
     char seps[] = "[]'`_*?0123456789()\",.! -:;/\t\n";
   
     char str[] = "  'Hello\tWorld!'  Foo\nBar  ";
     char str[80] = "  'Hello\tWorld!'  Foo\nBar  ";


     int count = 0;
     int count = 0;
29번째 줄: 28번째 줄:
   return 0;
   return 0;
}
}
// 0: [Hello]
</syntaxhighlight>
// 1: [World]
// 2: [Foo]
// 3: [Bar]
</source>


==같이 보기==
==같이 보기==
40번째 줄: 35번째 줄:
* [[C언어 strcpy()]]
* [[C언어 strcpy()]]
* [[C언어 문자열 배열]]
* [[C언어 문자열 배열]]
* [[함수 explode_with_whitespaces()]]


[[분류: C]]
[[분류:C 문자열]]
[[분류: stdio.h]]
[[분류:Stdio.h]]
[[분류: string.h]]
[[분류:String.h]]
[[분류:Stdlib.h]]

2023년 1월 28일 (토) 18:14 기준 최신판

1 개요[ | ]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUMBER_OF_STRINGS 200

int main() {
    char *words[NUMBER_OF_STRINGS];
    char seps[] = "[]'`_*?0123456789()\",.! -:;/\t\n";
    char str[] = "  'Hello\tWorld!'  Foo\nBar  ";

    int count = 0;
    char *token;
    token = strtok(str, seps);
    while( token != NULL ) {
        words[count] = malloc(strlen(token)+1);
        strcpy( words[count], token );
        count++;
        token = strtok(NULL, seps);
    }
    
    for(int i=0; i<count; i++) {
        printf("%d: [%s]\n", i, words[i]);
    }

   return 0;
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}