1 / 29

강의 내용 ( 아홉 번째 )

강의 내용 ( 아홉 번째 ). 오늘 강의 내용 (5 월 17 일 ) : 6 장 배열 문자 배열 예습 (5 월 24 일 ) 7 장 함수 숙제 없음. 6.4 문자배열. 6.4.1 문자배열. 문자열을 처리하는 방법에는 문자배열 (character arrays) 을 사용하는 방법과 포인터를 사용하는 방법이 있다 . 여기서는 배열을 이용한 방법만을 배우기로 한다. 문자열 상수. 문자열 상수는 “ …” 로 문자열을 묶어 주기만 하면 된다 .

Télécharger la présentation

강의 내용 ( 아홉 번째 )

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. 강의 내용(아홉 번째) • 오늘 강의 내용 (5월 17일) : 6장 배열 • 문자 배열 • 예습 (5월 24일) • 7장 함수 • 숙제 • 없음

  2. 6.4 문자배열

  3. 6.4.1 문자배열 • 문자열을 처리하는 방법에는 문자배열(character arrays)을 사용하는 방법과 포인터를 사용하는 방법이 있다. • 여기서는 배열을 이용한 방법만을 배우기로 한다.

  4. 문자열 상수 • 문자열 상수는 “…”로 문자열을 묶어 주기만 하면 된다. 주 소0x01 0x02 0x03 0x04 0x05 0x06 0x07 ASCII코드0x53 0x74 0x72 0x69 0x6E 0x67 0x00 문 자S t r i n g (NULL) (주소는 임의로 정한 주소임) • 문자열의 마지막에는 NULL 문자열이 들어간다.

  5. 문자열 변수 • C에서는 문자열 데이터형이 없으므로 문자 배열을 사용하여 문자열 처리를 한다. char 배열이름[문자열길이+1]=“초기문자열”;

  6. 문자열을 복사할 때는 strcpy함수를 이용하여야 하며, 직접 대입할 수 없다. • 직접 대입을 할 경우는 하나씩 배열의 요소에 넣어야 한다. char str[10] = "String"; strcpy(문자배열이름,문자열); strcpy(str,"Welcome!"); str="Hello!"; /*이렇게 사용할 수는 없다. */ str[0]='H';  /* 대입연산자를 사용할 경우는 일일이 이렇게 한 문자씩 대입한다. */ str[1]='e'; str[2]='l'; str[3]='l'; str[4]='o'; str[5]='!'; str[6]=NULL; /* 또는 str[6]='\0'; */

  7. 문자열 상수의 초기화 • const char str[10]="String"; • const char str[]="String";

  8. 예제 6.20문자열복사함수를 사용하는 방법과 하나의 배열 요소에 대입하는 방법. /* 파일 이름 : 문자열 처리 프로그램. */ #include <stdio.h> #include <string.h> void main(void) {        char str[10];        strcpy(str,"welcome");        printf("%s\n",str);        str[0]='W';        printf("%s\n",str); }

  9. 특수 문자 문자 기능 \a Beep음을 컴퓨터 스피커로 출력 \b Back space \n 현재 위치한 줄의 다음 줄로 내려간다. \r 현재 위치한 줄의 맨 처음으로 간다. \t 수평 Tab \v 수직 Tab \\ \(역슬래쉬) \‘ 작은 따옴표 \“ 큰 따옴표 \0 NULL문자 \0?? 8진수 ??에 대한 문자 \x?? 16진수 ??에 대한 문자.

  10. 예제 6.21특수문자 처리하는 방법. #include <stdio.h> void main(void) {        printf("C Programming\n");        printf("C \bProgramming\n");        printf("\'C\' Programming\n");        printf("\"C Programming\"\n");        printf("C \tProgramming\n");        printf("C \rProgramming\n");        printf("C\n\tProgramming\n");        printf("C Programming\a\n"); }

  11. 표 6.2문자검사 및 변환함수 6.4.2 문자분류 및 문자변환 표준함수

  12. 6.4.2 문자분류 및 문자변환 표준함수(계속)

  13. 예제 6.22toupper()를 사용하여 문자열을 한번에 한 문자씩 대문자로 변환하는 예제. #include <stdio.h> #include <ctype.h> void main(void) {    char name[80];    int loop;  printf("Enter in a name in lowercase\n");    scanf( "%s", name );    for( loop = 0; name[loop] != 0; loop++ )         name[loop] = toupper( name[loop] );  printf("The name in uppercase is %s", name); }

  14. 예제 6.23예제 6.22를 strupr함수를 사용하여 한꺼번에 대문자로 바꾸기 #include <stdio.h> #include <string.h> void main(void) {    char name[80]; /*declare an array of characters 0-79*/    printf("Enter in a name in lowercase\n");    scanf( "%s", name );    strupr( name );    printf("The name is uppercase is %s\n",name );    strlwr( name );    printf("The name is lowercase is %s\n",name ); }

  15. 보이는 문자(visible graphic character)

  16. 추가 그래픽 문자(Additional graphic characters)

  17. 제어문자(escape sequence)

  18. 숫자를 이용한 제어 문자(numerical escape sequence)

  19. 예제 6.24문자처리함수를 이용한 프로그램이다. 그 결과를 분석해보자 #include <stdio.h> #include <ctype.h> #include <conio.h> void main(void) {        int c;        printf("\n isalpha : \n");        for(c=0;c<127;c++)               if(isalpha(c))putchar(c);        printf("\n isdigit :\n");        for(c=0;c<127;c++)               if(isdigit(c))putchar(c);        printf("\n islower :\n");

  20. 예제 6.24(계속)        for(c=0;c<127;c++)               if(islower(c))putchar(c);        printf("\n ispunct :\n");        for(c=0;c<127;c++)               if(ispunct(c))putchar(c);        printf("\n isupper :\n");        for(c=0;c<127;c++)               if(isupper(c))putchar(c);        getch(); }

  21. 6.4.3 데이터 변환

  22. 예제 6.25문자열을 숫자 데이터형으로 변환하는 프로그램. 문자열은 계산이 안되지만 숫자로 변환한 후에는 계산이 가능하게 된다. #include <stdio.h> #include <stdlib.h> void main(void) {    char string1[] = "12.434";    char string2[] = "43.534";   char string3[] = "453", string4[] = "985";   /* printf("%s", string1 + string2); 이 부분은 컴파일 시 에러가 난다. 문자열은 덧셈을 할 수 없으며, 어떤 컴파일러에서는 되는 곳도 있다.*/

  23. 예제 6.25(계속)    printf("%f\n",atof(string1) + atof(string2)); /* 실수로 변환이 되었으므로 연산이 가능하다. */    /* printf("%s", string3 + string4); 마찬가지로 에러가 발생한다.*/    printf("%d\n",atoi(string3) + atoi(string4));    /* 정수로 변환이 되었으므로 연산이 가능하다. */ }

  24. 6.4.4 스트림 입출력

  25. 6.4.4 스트림 입출력(계속)

  26. 6.4.5 문자열 처리

  27. 6.4.5 문자열 처리(계속)

  28. 예제 6.28문자열 처리 함수를 이용한 프로그램이다. #include <stdio.h> #include <conio.h> #include <string.h> void main(void) {    char a[30],b[30],pass[10],s1[15],s2[15]; printf("/*strcat()=string1+string 2 */\n\n");    printf("input string 1:"); gets(a);    printf("input string 2:"); gets(b);    printf("\n\n string1 + string2 = %s \n", strcat(a,b));    printf("\n\n/* strcmp() = compare string1 with string2 */ \n");    printf("\n\nif your password is....h6614 \n");

  29. 예제 6.28(계속)    do{       printf("Your Password : "); gets(pass);       if(strcmp(pass,"h6614"))            printf("invalid password\n");       else            break;    } while(1);    printf("\n O.K");    getch();   printf("\n/*strcpy(s1,s2)=Copy s2 to s1*/ \n");    printf("input string s2 :"); gets(s2);    strcpy(s1,s2);    printf("s1 = %s\n",s1);    getch(); }

More Related