Presentation is loading. Please wait.

Presentation is loading. Please wait.

제 29 강 스트링(string) 다루기 s a i s . s T i h t g r i n.

Similar presentations


Presentation on theme: "제 29 강 스트링(string) 다루기 s a i s . s T i h t g r i n."— Presentation transcript:

1 제 29 강 스트링(string) 다루기 s a i s . s T i h t g r i n

2 스트링 문자열 문자열 상수: “Hello world” 문자열 변수? 문자의 배열로 선언 예: char hello[20];
printf 출력시, scanf 입력시 %s format 사용 printf("%s", hello); scanf("%s", hello); // Anything Wrong ?

3 초기화 char buf[10] = “Hello”; char mystring[] = “Hello world”;
main(){ buf2 = buf; // 이 것은 안된다. ***주의*** string은 assign이 안 된다. (초기화는 가능)

4 입출력 실습 lab29_01 파일명 str.c name은 10개짜리 char 배열로 선언한다.
scanf 를 이용하여 이름을 읽는다. 실패하면 종료한다. 성공하면 이름을 출력하고 계속 반복한다. Hint: while (scanf( .. ) == 1){ printf( ... ); }

5 유용한 함수들 strlen(str) 주어진 스트링의 길이를 반환 strcpy(dest, source)
스트링을 복제. 개념상 assignment와 같음 strcat(dest, source) dest뒤에 source를 붙임. strcmp(s1,s2) 두개의 스트링을 비교하여 사전순으로 s1이 빠르면 음수, 같으면 0, 뒤이면 양수를 돌려준다.

6 실습 lab29_02/strfun.c #include <string.h> char msg1[]=“Hello”;
char msg2[]=“world”; char buffer[20]; strlen을 이용하여 msg1의 길이가 얼마인지 출력하라 strcpy를 이용하여 buffer에 msg1을 복사한 뒤 buffer를 출력해보라 strcat을 이용하여 buffer의 뒤에 msg2를 덧붙인 뒤 buffer를 출력해보라

7 실습 lab29_03/strcmp.c scanf 를 이용하여 두개의 스트링을 입력한 다음 사전 순서상 빠른 것을 먼저 출력하고 느린 것을 뒤에 출력하는 프로그램을 작성하라. strcmp를 이용하여 msg1이 msg2보다 사전에서 앞에 있는지 뒤에 있는지를 출력하라. 최대 글자 수는 두 스트링 모두 20개로 하자.(선언할 때 필요) 출력 포맷 “%s 는 %s보다 앞에 있다.”

8 다른 값에서 스트링 만들기 char id[100]; int num;
sprintf(id, "student%d", num); // "student1", "student2", 등이 됨.

9 lab29_04/idgen.c char id[100]; 으로 선언하고 id 값을 최초에는 "id1" 로 만든다음 출력한다.
출력시에는 printf("%s\n", id); 로 하면 된다.

10 29 강 끝. 스트링


Download ppt "제 29 강 스트링(string) 다루기 s a i s . s T i h t g r i n."

Similar presentations


Ads by Google