๐Ÿ“—13์žฅ Strings | Exercises and Programming Projects

ํŒŒ์ธยท2022๋…„ 1์›” 18์ผ
0

C

๋ชฉ๋ก ๋ณด๊ธฐ
3/9
post-thumbnail
post-custom-banner

C Programming, A Modern Approach - K.N.KING ์˜ Exercises์™€ Programming Projects๋ฅผ ํ‘ผ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

๐Ÿ“ Exercises

  1. (b) %c๋Š” char์„ ๋ฐ›๋Š”๋‹ค. "\n"์€ ๋ฌธ์ž์—ด๋กœ ์ทจ๊ธ‰๋จ
    (c) %s๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ›๋Š”๋‹ค. '\n'์€ ๋ฌธ์ž(char) ์ทจ๊ธ‰๋จ
    (e) printf ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ›๋Š”๋‹ค.
    (h) putcharํ•จ์ˆ˜๋Š” ๋ฌธ์ž(char)์„ ๋ฐ›๋Š”๋‹ค.
    (i) putsํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ›๋Š”๋‹ค.
    (j) puts๋Š” ๋ฌธ์ž์—ด์„ ์“ด ํ›„ ๊ฐœํ–‰๋ฌธ์ž๋„ ์ถ”๊ฐ€๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค
    ๋”ฐ๋ผ์„œ ๊ฐœํ–‰์ด 2๋ฒˆ ๋œ๋‹ค.


  2. (a) putchar์€ char๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.
    (d) puts๋Š” ํฌ์ธํ„ฐ(์—ฌ๊ธฐ์„œ๋Š” char*)์„ ๋ฐ›๋Š”๋‹ค.

  3. (i) 12
    (s) abc34
    (j) 56

#include <stdio.h>
#include <ctype.h> // isspace ์‚ฌ์šฉ

int read_line(char str[], int n)
{
	int ch;
	int i = 0;

	while (isspace(getchar()));

	while ((ch = getchar()) != '\n' && !isspace(ch)) {
		if (i < n) {
			str[i++] = ch;
		}
	}
	str[i] = '\0';
	return i;
}
(a) 
void capitalize(char s[])
{
	for (int i = 0; s[i] == '\n'; i++) {
		toupper(s[i]);
	}
}
(b) 
void capitalize(char *s)
{
	for (; *s == '\n'; s++) {
		isupper(*s);
	}
}
void censor(char *s)
{
	for (; *s == '\n'; s++) {
		if (*s == 'f' && *(s + 1) == 'o' && *(s + 2) == 'o') {
			*s = *(s+1) = *(s+2) = 'x';
		}
	}
}
  1. (d)

  2. tired-or-wired?\0

  3. computers\0

  4. q๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด์€ ํ•จ์ˆ˜ ๋ฒ”์œ„ ๋ฐ–์—์„œ๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.

int strcmp(char* str1, char* str2)
{
    while (*str1 == *str2) {
        if (*str1 == '\0') {
            return 0;
        }
        str1++; 
        str2++;
    }
    return *str1 - *str2;
}
void get_extension(const char* file_name, char* extension)
{
	while (*file_name) {
		if (*file_name++ == '.') {
			strcpy(extension, file_name);
		}
	}
}
void build_index_url(const char* domain, char* index_url)
{
	strcpy(index_url, "http://www");
	strcat(index_url, domain);
	strcat(index_url, "/index.html");
}
  1. Grinch

  2. (a) 3 (b) 2 (c)

int count_space(const char* s)
{
	int count = 0;

	while (*s) {
		if (*s == ' ') {
			count++; 
			s++;
		}
	}

	return count; 
}
bool text_extension(const char* file_name, const char* extension)
{
	while (*file_name != '.') {
		file_name++;
	}
	file_name++;

	while (*file_name != '\0' && *extension != '\0')
	{
		if (toupper(*extension++) != toupper(*file_name++)) {
			return false;
		}
	}
	return true;
}
void remove_filename(char* url)
{
	while(*url++);
	while (*url-- != '/');
	url++;
	*url = '\0';
}
profile
๊ณต๋ถ€์ •๋ฆฌ์šฉ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€