1. 단어 추출 프로그램

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
 
char *my_token(char *ps);
//char형 주소를 반환하고 char형 포인터 매개변수를 갖는 함수
int main()
{
    char str[80];
    char *p;
    
    printf("문장 입력 : ");
    gets(str);
    
    while((p =my_token(str)) !=NULL)//한문자씩 출력 
    {
    printf("%s\n",p);
    }
    //str이 NULL이 올때가지 실행 즉, 문자열의 끝(NULL)까지 실행 
    return 0;
}
 
char *my_token(char *ps)
{     
    static char* ch = NULL;
    static int end =0;      //정적 지역변수 선언
    char *start; 
    char *count;
    
    if(ps==NULL)
    return NULL;            //문자열이 있는지?
    
    if(end == 1//끝났는지? 
    return NULL;
    
    if(ch ==NULL)//시작  
    start = ps;
    else
    start = ch+1;
    
    count = start;
    
    while(*count !=' '&&*count !='\0')
    count++;
    // 공백 까지 counting
    if(*count =='\0'end =1;//끝 
    else
    *count = '\0';//구분했을때 공백에 null 
    
    ch = count;//위치 넣어줌. 
    
    return start; 
}
cs



■명령 인수


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>
#include<string.h>
int main(int argc, char **argv) {
    char str[80];
    int num1, num2;
    int i;
    int count;
    int sum=0;
    //cmd 에서 입력 예 : atio 123 4
    //                 ->  argv[0] argv[1] argv[2]
    strcpy(str,argv[1]);
    count = atoi(argv[2]);
    //문자열을 정수로 넣어준다. 
    for (i=0;i<count;i++) {
        num1 = atoi(str);
        printf("\n%s\n",str);
        strrev(str);
        printf("%s\n",str);
        printf("--------\n");
        sum =num1+atoi(str);
        //문자 -> 정수 
        printf("%d\n",sum);
        itoa(sum, str, 10);
    }
}
//atio.exe(실행파일을) cmd의 디렉토리에 마춰서(C:\)넣은후 실행
cs




+ Recent posts