C language
-
malloc() 사용법 (malloc() usage)C language 2017. 8. 28. 01:08
malloc() 를 이용한 간단한 코드를 작성했다.I wrote simple code using the malloc function.#include int * make_array(int elem, int val); void show_array(const int ar[], int n); int main(void) { int *pa; int size; int value; printf("원소의 개수를 입력하시오: "); while(scanf("%d",&size) == 1 && size > 0) { printf("초기화 값을 입력하시오: "); scanf("%d",&value); pa = make_array(size, value); if(pa) { show_array(pa, size); } printf("원소의..
-
C언어 난수 만들고 정렬하기(Creating and sorting C language random numbers)C language 2017. 8. 26. 01:36
함수 rand()에 의해서 생성되는 정수범위는 0 부터 n 까지의 임의의 정수이다. (n은 정해져있지만 시스템에 따라 상이함)그러나 일반적으로 rand()만 사용하게 되면 , 같은 숫자가 계속 나오게 된다. 그래서 즉, 시드값 (seed value)를 넣어줘야 숫자가 바뀌는데, 시드값을 넣어주기 위해서 srand()를 사용하고시드값 또한 자동으로 넣기위해서 time() 함수를 사용한다. time() 함수는 1970년 1월 1일 이후의 경과된 시간을 초 단위로 반환하는 함수다.그리고 만들어진 난수를 1~10 까지 숫자로 한정시키기위해 (rand() % 10) +1 을 사용했다. 예를들어 rand() % 10 을 사용하면 0~9 까지의 난수가 나오고 10~ 19 사이의 난수를 원한다면 (rand() %10 ..
-
자신이 호출된 횟수를 리턴하는 함수(A function that returns the number of times it has been called)C language 2017. 8. 24. 02:29
자신이 호출된 횟수를 리턴하는 함수이다.A function that returns the number of times it has been called 재귀함수를 이용했을때.When using Recursive function. #include int nothing(int ); int main(void) { int call; int times; puts("How many times do you want to run the function?(to exit 'q')"); while(scanf("%d",×) == 1) { call = nothing(times); printf("The function executed %d times\n",call); call = 0; puts("How many times do ..
-
C언어 명령행 전달인자 예제(C language command line argument example)C language 2017. 8. 20. 00:08
파일끝까지 입력을 읽고 명령에 맞춰서 출력하는 프로그램이다.It reads the input to the end of the file and outputs it according to the command. #include #include #include #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], ..
-
ctype.h 을 활용한 예제(Example using ctype.h)C language 2017. 8. 19. 01:27
이 프로그램은 문장을 입력하면 문장내의 단어, 대문자, 소문자, 숫자, 구두점 수를 세어준다.This program will count the number of word,uppercase letter, lowercase letter, number, punctuation in the sentence after you enter sentence#include #include #define SIZE 50 void decide (char *); char *sgets (char*, int n); int main(void) { char st[SIZE]; puts("Enter any sentence!: (Enter to exit)"); while(sgets(st, SIZE) != NULL && st[0] != '\0..
-
C언어 문자열내 빈칸(스페이스) 지우기(remove space in string)C language 2017. 8. 18. 02:57
문자열내의 스페이스 (빈칸) 을 지우는 프로그램을 작성하여 간단하게 테스트 해보았다.Write code to remove space in string and then tested it simply. #include #include void remove_space (char *, unsigned long); char *sgets (char *, int n); int main(void) { char ar[50]; unsigned long n; puts("Enter any sentence : (to exit just push enter)"); while (sgets(ar,50) != NULL && ar[0] != '\0') { n = strlen(ar); remove_space(ar,n); puts("Ente..
-
C언어 문자열 거꾸로 바꾸기 (Inverts the string)C language 2017. 8. 17. 22:12
j = n-1; 이부분을 보면 -1 을 하지 않을 경우 temp[0]에 '\0' 종료 문자가 들어가므로 , 아무것도 출력하지 않는다. without -1, temp[0] gets '\0' and nothing is output. #include #include // strlen() 사용을 위해 (for use strlen) void reverse (char *, unsigned long); char *sgets (char *, int n); int main(void) { char ar[50]; unsigned long n; puts("Enter any sentence : (to exit just push enter)"); while (sgets(ar,50) != NULL && ar[0] != '\0') ..
-
문자열내에서 문자찾기C language 2017. 8. 11. 16:18
#include void string_in (char [], char []); char s_gets (char *st, int n); #define NUM 50 int main(void) { char first[NUM] = "a"; char second[NUM]; while (first[0] != '\0') { puts("첫번째 문자열을 입력하세요:(끝내려면 엔터)"); s_gets(first, NUM); puts("두번째 문자열을 입력하세요:(끝내려면 엔터)"); s_gets(second, NUM); string_in (first,second); } } char s_gets (char *st, int n) { char *rv; rv = fgets(st, n, stdin); int count = 0; i..