8. 1 배열 Ch 08. 배열과 문자열 o 따라하기 2 #include <stdio. h> #define M 3 // 행의 갯수 #define N 4 // 열의 갯수 int main(void) { int a[M][N], i, j, sum = 0; for ( i = 0; i < M; ++i ) for ( j = 0; j < N; ++j ) a[i][j] = i + j; // element에 값을 할당 for ( i = 0; i < M; ++i ){ // element 값을 행 단위로 출력 for ( j = 0; j < N; ++j ) printf("a[%d] = %d ", i, j, a[i][j] ); printf("n"); } 12
Ch 08. 배열과 문자열 8. 3 문자열과 문자열 배열 o 간단한 텍스트 편집기 #include <stdio. h> #define MAX 100 #define LEN 80 char text[MAX][LEN]; /* 매우 간단한 텍스트 편집기 */ void main(void) { register int t, i, j; printf("Enter an empty line to quit. n"); for(t=0; t<MAX; t++) { printf("%d: ", t); gets(text[t]); if(!*text[t]) break; /* 빈 라인이면 break */ } for(i=0; i<t; i++) { for(j=0; text[i][j]; j++) putchar(text[i][j]); putchar('n'); } } 24
8. 5 문자열 처리 함수 Ch 08. 배열과 문자열 o 따라하기 7 #include <stdio. h> #include <string. h> void main(void) { char s 1[80], s 2[80]; gets(s 1); gets(s 2); } printf("lengths: %d %dn", strlen(s 1), strlen(s 2)); if(!strcmp(s 1, s 2)) printf("The strings are equaln"); strcat(s 1, s 2); printf("%sn", s 1); strcpy(s 1, "This is a test. n"); printf(s 1); if(strchr("hello“, 'e')) printf("e is in hellon"); if(strstr("hi there", "hi")) printf("found hin"); string Enter string 1 Enter // strcmp()는 비교된 문자열이 같으면 거짓(0)을 리턴 //그러므로 같음을 시험하기 위한 조건에서는 이것의 역으로 하는 //논리 연산자 !를 사용 33