C Programming, A Modern Approach - K.N.KING ์ Exercises์ Programming Projects๋ฅผ ํผ ๋ด์ฉ์ ๋๋ค.
#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';
}
}
}
(d)
tired-or-wired?\0
computers\0
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");
}
Grinch
(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';
}