1 / 19

10 장 . 문자열

10 장 . 문자열. 문자열의 구조. char ch[5] = {'A', 'C', '<br>', 'h', '07'};. 문자열의 구조. char ch[5] = {'A', '<br>', 'h'}; char ch[5] = {'A', '<br>', 'h', ' ', ' '}; char name[6] = &quot;Korea&quot;; char name[6] = {'K', 'o', 'r', 'e', 'a', ' '};. 문자열의 구조. printf(&quot;%s&quot;, name); // 아래 모두 Korea 출력

simone
Download Presentation

10 장 . 문자열

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 10장. 문자열 Ch.10

  2. 문자열의 구조 • char ch[5] = {'A', 'C', '\n', 'h', '\007'}; Ch.10

  3. 문자열의 구조 • char ch[5] = {'A', '\n', 'h'}; • char ch[5] = {'A', '\n', 'h', '\0', '\0'}; • char name[6] = "Korea"; • char name[6] = {'K', 'o', 'r', 'e', 'a', '\0'}; Ch.10

  4. 문자열의 구조 • printf("%s", name); // 아래 모두 Korea 출력 • char name[6] = "Korea"; • char name[10] = {'K','o','r','e','a' }; • char name[10] = {'K','o','r','e','a','\0','A','s','i','A' }; Ch.10

  5. 문자열의 구조 #include <stdio.h> void prt_str(char ary[]); void main() { char name1[6] = "Korea"; /* 널문자 자동 추가 */ char name2[10] = { 'K', 'o', 'r', 'e', 'a' }; /* 0으로 초기화 */ char name3[10] = { 'K', 'o', 'r', 'e', 'a', '\0', 'A', 's', 'i', 'A' }; prt_str(name1); prt_str(name2); prt_str(name3); } void prt_str(char ary[]) { inti=0; while (ary[i] != '\0') /* 또는 while(ary[i]) */ putchar(ary[i++]); putchar('\n'); } Ch.10

  6. 문자열의 입출력 #include <stdio.h> void main() { char name[15]; printf("Enter your name\n"); gets(name); /* name의 위치에 문자열을 저장 */ printf("Hi! %s \n", name); } // gets – 한 행(‘\n’을 만날 때까지)을 입력받아 저장 입력 : kim Insoo ? Ch.10

  7. 문자열의 입출력 #include <stdio.h> void main() { char name[] = "Kim Insoo"; puts(name); puts("Kim Insoo"); } // puts 실행 후 자동적으로 다음 행에 위치 Ch.10

  8. 문자열의 입출력 #include <stdio.h> void main() { char name[] = "Kim Insoo"; printf("%s", name); printf("%s", "Kim Insoo"); } Ch.10

  9. 문자열 함수( strlen(s) ) • #include <string.h> /* 문자열 함수 정의 포함 */ #include <stdio.h> #include <string.h> void main() { char str[] = "Window XP"; printf("Length of \"%s\" is %d. \n", str, strlen(str) ); } Ch.10

  10. 문자열 함수( strlen(s) ) #include <stdio.h> #include <string.h> void main() { // 문자 순서를 거꾸로 바꾸기 char str[80], temp; int i, j, len; gets(str); len = strlen(str); for (i=0, j=len-1; i<j;i++, j--) { temp = str[i]; /* str[i]의 값을 temp에 보존 */ str[i] = str[j]; str[j] = temp; /* temp에 보존된 값으로 대입 */ } puts(str); } Ch.10

  11. 문자열 함수( strcat(s1, s2) ) #include <stdio.h> #include <string.h> void main() { char str1[20]; char str2[10]; printf("First name? "); gets(str1); /* 이름을 입력 */ printf("Last name? "); gets(str2); /* 성을 입력 */ strcat(str1, str2); // 문자열 str2를 str1에 결합후 str1에 저장 printf("Your full name is %s \n", str1); printf("Your last name is still %s \n", str2); } Ch.10

  12. 문자열 함수( strcpy(s1, s2) ) #include <stdio.h> #include <string.h> void main() { char str1[15]; char str2[] = "Hong Gildong"; strcpy(str1, str2); //문자열 str2를 복사하여 str1에 저장 puts(str1); strcpy(str1, "Kim Chulsoo"); puts(str1); // Kim Chulsoo } Ch.10

  13. 문자열 함수( strcpy(s1, s2) ) • (x) char name[15]; name = "Kim Chulsoo"; • (o) strcpy(name, "Kim Chulsoo"); Ch.10

  14. 문자열 함수( strcmp(s1, s2) ) • s1 이 s2 보다 사전적 순서에서 앞에 위치하면 : 음의 정수값 • s1 과 s2 가 같은 문자열이면 : 0 • s1 이 s2 보다 사전적 순서에서 뒤에 위치하면 : 양의 정수값 Ch.10

  15. 문자열 함수( strcmp(s1, s2) ) #include <stdio.h> #include <string.h> void main() { printf("%d \n", strcmp("abc", "abc")); /* 같음(0 반환) */ printf("%d \n", strcmp("abc", "abe")); /* ‘c’-’e‘반환, -2*/ printf("%d \n", strcmp("abc", "aaf")); /* ‘b’-‘a’반환, 1*/ printf("%d \n", strcmp("abc", "ab")); /* ‘c’-’\0‘반환, 99*/ } Ch.10

  16. 문자열의 배열 • char natn[][8]={"Korea", "Japan", "France", "Germany"}; Ch.10

  17. 문자열의 배열 • for (i=0; i<4; i++) puts(natn[i]);/* 또는printf("%s \n", natn[i]); */ • natn[0] == &natn[0][0] natn[1] == &natn[1][0] natn[2] == &natn[2][0] natn[3] == &natn[3][0] • strcpy(natn[1], natn[2]); Ch.10

  18. 문자열의 배열 #include <stdio.h> #include <string.h> void main() { char trouble[][4] = { "JAN", "FEB", "MAR" }; int i; for (i=0; i<3; i++) puts(trouble[i]); puts("----------"); strcpy(trouble[0], "SEPTEMBER"); /* 또는 gets()로 입력 */ for (i=0; i<3; i++) puts(trouble[i]); } Ch.10

  19. 문자열의 배열 • strcpy() 실행 전 J A N ‘\0’ F E B ‘\0’ M A R ‘\0’ • strcpy() 실행 후 S E P T E M B E R ‘\0’ R ‘\0’ Ch.10

More Related