-
[Kernighan - C] #2-8 증가 연산자와 감소 연산자전기전자공학/프로젝트 2017. 6. 25. 21:46
2.8 증가 연산자와 감소 연산자
The C Programming - Kernighan
<예제>
[예제 2-4]
문자열 s2의 문자 중 문자열 s1에 있는 문자들을 지우는 squeeze(s1, s2)를 작성하라
squeeze.c
12345678910111213141516171819202122232425262728293031323334353637383940/** Filename: squeeze.c* Author: Ssong24 <pub50@naver.com>* Date: 25-June-2017** The C Programming Language, Second Edition, Korean ver* by Kernighan and Ritchie** Exercise 2-4, page 64*/#include <stdio.h>#define TRUE 1#define FALSE 0void squeeze(char s1[], char s2[]) {int i, j;for (i = j = 0; s1[i] != '\0'; i++)if (!check(s1[i], s2))s1[j++] = s1[i];s1[j] = '\0';}int check(char ch, char s2[]) {int state = FALSE;int i;for (i = 0; i < strlen(s2); i++){if (ch == s2[i]) {state = TRUE;return state;}else state = FALSE;}return state;}cs main.c
123456789101112131415161718192021222324252627282930#include <stdio.h>#define MAXLINE 1000int getline(char line[], int maxline);main() {char str1[MAXLINE];char str2[MAXLINE];getline(str1, MAXLINE);getline(str2, MAXLINE);squeeze(str1, str2);printf("%s\n", str1);}int getline(char s[], int lim){int c, i;for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)s[i] = c;if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;}cs [예제 2-5]
문자열 s1에서 문자열 s2에 있는 문자들 중 하나가 나타나는 지를 찾아서 처음 나타나는 것의 위치(s1 내에서의 위치)를 리턴하고,
없을 경우 -1을 리턴하는 함수 any(s1, s2)를 작성하라
(표준 라이브러리 함수 strpbrk는 같은 작용을 하고, 단지 그 위치에 대한 포인터를 리턴한다.)
'전기전자공학 > 프로젝트' 카테고리의 다른 글
[Kernighan - C] #2 - 10 지정 연산자와 수식 (0) 2017.06.25 [Kernighan - C] #2-9 비트 연산자 (0) 2017.06.25 [Kernighan - C] #1-10 외부변수 (0) 2017.06.24 [Kernighan - C] 1장 언어소개 - (5. 문자 입출력) (0) 2017.06.24 [Kernighan - C] 1장 언어소개 - (4. 상수정의) (0) 2017.06.24