-
C언어 명령행 전달인자 예제(C language command line argument example)C language 2017. 8. 20. 00:08SMALL
파일끝까지 입력을 읽고 명령에 맞춰서 출력하는 프로그램이다.
It reads the input to the end of the file and outputs it according to the command.
#include <stdio.h> #include <string.h> #include <ctype.h> #define OPTION "option" void cupper (int argc, char *argv[]); void clower (int argc, char *argv[]); int main(int argc, char * argv[]) { int count = 0; if(argc == 1) printf("Usage: %s any sentences option (-q, -u, -l)\n",argv[0]); else if (strcmp(argv[1], OPTION) == 0 && argc == 2) { puts(" -q : Output the input as it is."); puts(" -u : Output the input after changing it to all uppercase."); puts(" -l : Output the input after changing it to all lowercase."); } else if (argv[argc-1][0] == '-' ) { if(argv[argc-1][1] == 'q') { for(count = 1; count < argc-1; count++) printf("%s ",argv[count]); puts(""); } else if(argv[argc-1][1] == 'u') { cupper(argc,argv); for(count = 1; count < argc-1; count++) printf("%s ",argv[count]); puts(""); } else if(argv[argc-1][1] == 'l') { clower(argc,argv); for(count = 1; count < argc-1; count++) printf("%s ",argv[count]); puts(""); } } else { for(count = 1; count < argc; count++) printf("%s ",argv[count]); puts(""); } return 0; } void cupper (int argc, char *argv[]) { int i,j; for(i = 1; i < argc-1; i++) for(j = 0; j < strlen(argv[i]); j++) if(islower(argv[i][j])) argv[i][j] = toupper(argv[i][j]); return ; } void clower (int argc, char *argv[]) { int i,j; for(i = 1; i < argc-1; i++) for(j = 0; j < strlen(argv[i]); j++) if(isupper(argv[i][j])) argv[i][j] = tolower(argv[i][j]); return ; }
아무것도 입력하지 않았을때에는 사용법이 출력된다.
If you do not enter anything, print usage.
'옵션' 을 입력했을때는 옵션정보를 출력한다.
If you enter 'option', print information of option
옵션을 붙이지 않았을때는 -q 옵션으로 출력되는것을 기본으로 한다.
If you do not enter with option, the output is based on the -q option.
-u 옵션은 모든 문자를 대문자로 변경한뒤 출력하고 -l 옵션은 모든 문자를 소문자로 변경하여 출력한다.
The -u option changes all characters to uppercase and prints them, and the -l option changes all characters to lowercase.
BIG'C language' 카테고리의 다른 글
C언어 난수 만들고 정렬하기(Creating and sorting C language random numbers) (0) 2017.08.26 자신이 호출된 횟수를 리턴하는 함수(A function that returns the number of times it has been called) (0) 2017.08.24 ctype.h 을 활용한 예제(Example using ctype.h) (0) 2017.08.19 C언어 문자열내 빈칸(스페이스) 지우기(remove space in string) (0) 2017.08.18 C언어 문자열 거꾸로 바꾸기 (Inverts the string) (0) 2017.08.17