char
constant, string
constant, int
vs string
,strlen
, strcpy
, strcmp
, strtok
, sprintf
, atoi
string.h
int
, char
, string
์ ์ ์, ์ ์ฅ, ์ฝ๊ธฐ, ์ถ๋ ฅ, ๋น๊ตstrtok
string
์ ์ ์string
์ ๋ฐฐ์ด์ ๋์ด 0
์ผ๋ก ๋๋๋ character array์ด๋ค. (์์ธํ ์ค๋ช
)string
์ ๋์๋ ํญ์ ๋ณด์ด์ง ์๋ 0
์ด ์จ๊ฒจ์ ธ ์๋ค๋ ๊ฒ์ ๊ธฐ์ตํด์ผ ํ๋ค.char
constant (๋ฌธ์ ์์)char
constant (๋ฌธ์ ์์)๋ compile time ๋์ ascii number๋ก ๋์ฒด๋๋ค.x = 'a';
// The compiler changes above to
x = 97; // 97 is the ascii number of 'a'
string
constant (๋ฌธ์์ด ์์)string
constant (๋ฌธ์์ด ์์)๋ compile time ๋์ ์ ์ฅ๋ ์ฃผ์๋ก ๋์ฒด๋๋ค. (replaced by the address where it is stored.)string
constant๋ ์ปดํ์ผ๋ฌ์ ์ํด ๋ฉ๋ชจ๋ฆฌ์ โstring areaโ(๋ฌธ์์ด ์์ญ)์ ์ ์ฅ๋๋ค.char * x= "hello";
// The compiler changes above to (if "hello" is stored at 0x400640)
char * x = 0x400640;
strcpy
strlen
strcmp
strtok
char x[10]; // x is 10 bytes where we can store char's
strcpy(x, "hello"); // we store 6 bytes ('h','e','l','l','o',0) into x array
char *y; // y is 4 bytes where we can store an address
y = "hello"; // we store address 0x400640 in x (assuming this is the address of "hello")
malloc
or new
malloc
, C++์ new
๋ฅผ ์ฌ์ฉํ๋ค.char *x;
strcpy(x, "hello"); // this is an error.
// we need data space to copy "hello",
// but x has no space for data.
// x has space only for address.
x=(char *)malloc(10); // allocate space first and store the address of it in x
strcpy(x, "hello"); // and then store data.
// "hello" is stored in the location pointed by x
char a[5];
for(i=0;i<5;i++){
a[i]='q';
}
char b[5];
for(i=0;i<3;i++){
b[i]='q';
}
b[3]=0;
printf
๋ฅผ ํตํด ์ถ๋ ฅํ ์ ์๋ค.printf("%s", b);
%s
๋ โbโ๊ฐ ๋ฌธ์์ด์ ์ฃผ์์์ ์๋ฏธํ๋ฉฐ, ํด๋น ์ฃผ์๋ก ์ด๋ํ ํ 0์ด ํ์๋ ๋๊น์ง ํด๋น ์ฃผ์์์ ์์ํ๋ ๋ชจ๋ ๋ฌธ์๋ฅผ ์ธ์ํ๋ค.for(i=0;i<5;i++)
printf("%c",a[i]);
printf("%s", a);
๋ฅผ ์ฌ์ฉํ๋ค๋ฉด, ์ปดํจํฐ๋ โaโ๊ฐ ๋ฌธ์์ด์ ์ฃผ์๋ผ๊ณ ์๊ฐํ๊ณ ๊ทธ ์ฃผ์๋ก ๊ฐ์ 0์ด ๋ณด์ผ ๋๊น์ง ๊ทธ ์ฃผ์์์ ์์ํ๋ ๋ชจ๋ ๋ฌธ์๋ฅผ ์ธ์ํ ๊ฒ์ด๋ค.
int
, char
, string
์ ์ ์, ์ ์ฅ, ์ฝ๊ธฐ, ์ถ๋ ฅ, ๋น๊ตint x; // x is a room for an integer
x = 10; // we can store an integer in x
scanf("%d", &x); // or we can read an integer into x
printf("%d", x); // print an integer
if (x==10){ // check the value of x
......
}
char y; // y is a room for a character
y = 't'; // we can store a character in y
scanf("%c", &y); // or we can read a character into y
printf("%c", y); // print a character
if (y=='t'){ // check the value of y
......
}
char z[50]; // z is a 50-room space for a string
strcpy(z, "korea"); // we have to use strcpy() to store a string in z
scanf("%s", z); // or we can read a string into z
printf("%s", z); // print a string
int k;
k=strcmp(z, "korea"); // if equal, strcmp returns 0;
// otherwise strcmp returns non-zero
if (k==0){ // check the value of z
........
}
๋ฌธ์์ด์ ์ฌ์ค์ 0์ผ๋ก ๋๋๋ character array์ด๋ค.
for(i=0;;i++){
printf("%d-th:char %c ascii %d\n", i, z[i], z[i]);
if (z[i]==0){
break;
}
}
strlen()
์ ์ฌ์ฉํ๋ฉด string์ length๋ฅผ ์ถ๋ ฅํ ์ ์๋ค.
printf("the length of this string is %d\n", strlen(z));
strtok
strtok
์ ๋ฌธ์ฅ์ ๋จ์ด๋ค์ ๋ฐฐ์ด๋ก split ํด์ค๋ค.strtok(buf, " ");
์ โbufโ์์ ์ฒซ ๋ฒ์งธ ๋จ์ด๋ฅผ ์ฐพ๊ณ ๊ทธ ๋ค์ 0
์ ์ฝ์
ํ๋ค.
strtok(NULL, " ");
์ โbufโ์์ ๊ทธ ๋ค์ ๋จ์ด๋ฅผ ์ฐพ๊ณ ๊ทธ ๋ค์ 0
์ ์ฝ์
ํ๋ค.char buf[50];
strcpy(buf, "ab c def gh"); //buf contains (a, b, space, c, space, d, e, f, space, g, h, 0)
char *temp;
temp=strtok(buf, " "); // buf is now (a, b, 0, c, space, d, e, f, space, g, h, 0)
// and temp has the address of buf[0].
temp=strtok(NULL, " "); // buf is now (a, b, 0, c, 0, d, e, f, space, g, h, 0)
// temp has the address of buf[3]
temp=strtok(NULL, " "); // buf is now (a, b, 0, c, 0, d, e, f, 0, g, h, 0)
// temp has the address of buf[5]
example code:
char buf[256];
char *token;
printf("enter a sentence\n");
fgets(buf, 255, stdin); // read a line (maximum size 255)
buf[strlen(buf)-1]=0; // remove enter key
token = strtok(buf, " "); // get the first token
for(;;){
printf("%s\n", token);
token = strtok(NULL, " "); // get the next token
if (token==NULL) break;
}
char * x[10];
x[0]="hi"; // store address of "hi" in x[0]
strcpy(x[1],"bye"); // this is an error
x[1]=(char *)malloc(10); // allocate space first
strcpy(x[1], "bye"); // then store data
#include <stdio.h>
#include <string.h> // you need this header file for string functions
void main(){
char x, y;
x='a'; y=97;
printf("%d %c %d %c\n", x, x, y, y);
char * x1 = "hello";
printf("%s %p %s %p\n", x1, x1, "hello", "hello"); // use %p for address
}
Result is
97 a 97 a
hello 0x10879ff97 hello 0x10879ff97
In the first line of result, since %d
was used to print as decimal numbers, so 97
was output, and %c
was to print as one character, so a
was output.
In the second line, since %s
was used to print as strings, so hello
was output, and %p
was to print as the address of pointer, so 0x10879ff97
was output.
char x[10]; // x is a character array with 10 rooms
x[0]='a'; x[1]='b'; x[2]='c'; x[3]='d'; x[4]='e';
x[5]='f'; x[6]='g'; x[7]='h'; x[8]='i'; x[9]='j';
int i;
for(i=0;i<10;i++){
printf("%d %c \n", x[i], x[i]); // print each character with its ascii number
}
Result is
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
It means the ASCII number corresponding to each alphabet.
char x[10]; // x is a character array with 10 rooms
int i;
for(i=0;i<10;i++){
x[i]=i+97;
}
for(i=0;i<10;i++){
printf("%d %c \n", x[i], x[i]); // print each character with its ascii number
}
Result is
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
Since the result is the same compared to Problem 2, it can be seen that each ASCII number (e.g., 97) and each alphabet (e.g., a) are exactly the same.
char x[128];
for(int i=0;i<128;i++){
x[i]=i;
}
for(int i=0;i<128;i++){
printf("%d%c%d\n", x[i], x[i], x[i]);
}
strlen
] Read a string and display its length.Enter a string
hello
The length is 5
Enter a string
hello
h
e
l
l
o
char x[10];
strcpy(x, "hello");
strcpy(x, "hi");
for(int i=0;i<10;i++){
printf("%d ", x[i]);
}
104 105 0 108 111 0 -2 127 0 0
char x[10];
๋ฅผ ํตํด ๊ธธ์ด๊ฐ 10์ธ x๋ผ๋ ๋ฐฐ์ด์ ์ ์ธํ๋ฏ๋ก์ ๊ฐ๊ฐ์ ๋ฐฐ์ด์๋ ์๋ฏธ๊ฐ ์๋ ๊ฐ์ธ ์ฐ๋ ๊ธฐ๊ฐ์ด ๋ค์ด๊ฐ๊ฒ ๋๋ค.
-12 15 32 124 -14 129 -2 127 0 0
strcpy(x, "hello");
๋ฅผ ํตํด ๋ฐฐ์ด์ 0๋ฒ index๋ถํฐ 5๋ฒ index๊น์ง ๊ฐ๊ฐ โhello\0โ์ ํด๋นํ๋ ASCII ์ฝ๋ ๊ฐ์ธ 104 101 108 108 111 0
์ด ์ฑ์์ง๋ค.
104 101 108 108 111 0 -2 127 0 0
strcpy(x, "hi");
๋ฅผ ํตํด ๋ฐฐ์ด์ 0๋ฒ index๋ถํฐ 2๋ฒ index๊น์ง ๊ฐ๊ฐ โhi\0โ์ ํด๋นํ๋ ASCII ์ฝ๋ ๊ฐ์ธ 104 105 0
์ด ์ฑ์์ง๋ค.
104 105 0 108 111 0 -2 127 0 0
strlen
, strcmp
] Write a program that keeps reading a string, displaying its length, and checking whether it is โhelloโ. If the input string is โhelloโ, the program stops.Enter a string
hi
You entered hi. length=2
No it is not hello
Enter a string
hello
You entered hello. length=5
Yes it is hello. Bye.
strcpy
] Read a string and copy it to three other string variables and change the first letter of them to โaโ, โbโ, and โcโ, respectively, and display them.Enter a string
hello
After copying and changing the first letter
aello bello cello
char *x, *y, *z;
x="hello"; y="hi"; z="bye";
printf("%s %s %s\n", x, y, z);
printf("%p %p %p\n", x, y, z);
printf("%s %s %s\n", x, y, z);
์์ %s
๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด์ ์ถ๋ ฅํ๋ผ๊ณ ํ์์ผ๋ฏ๋ก, ์คํ ๊ฒฐ๊ณผ๋ก๋ x, y, z๊ฐ ๊ฐ์ง ๊ฐ๊ฐ์ ๋ฐฐ์ด์ ๊ฐ ์ฆ, hello hi bye
๊ฐ ์ถ๋ ฅ๋๋ค.printf("%p %p %p\n", x, y, z);
์์ %p
๋ฅผ ์ฌ์ฉํ์ฌ ์ฃผ์๋ฅผ ์ถ๋ ฅํ๋ผ๊ณ ํ์์ผ๋ฏ๋ก, ์คํ ๊ฒฐ๊ณผ๋ก๋ x, y, z ๊ฐ๊ฐ์ ์ฃผ์๊ฐ์ด ์ถ๋ ฅ๋๋ค. (e.g. 0x108c5df96 0x108c5df9c 0x108c5df9f
) char x[20];
strcpy(x, "hello"); // this is ok
x="hello"; // this is an error. "hello" is an address and we can't store address in
// x which is not a pointer variable
10.c:7:6: error: array type 'char [20]' is not assignable
๋ผ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค. x="hello"
์์ โhelloโ๋ ์ฃผ์๊ฐ์ด๊ณ x๋ ํฌ์ธํฐ ๊ฐ์ด ์๋ ๋จ์ํ ๋ฐฐ์ด์ ์๋ฏธํ๋ค. ๋ฐฐ์ด์์๋ assign(=
)์ ์ฌ์ฉํ ์ ์๋ค.strcpy
] Try below and explain why we have an error. How can you fix it? char *y;
y="hello1"; // this is ok
strcpy(y, "hello2"); // error because y has no space for "hello2"
y
๊ฐ ํฌ์ธํฐ ๋ณ์์ด๊ธฐ ๋๋ฌธ์ strcpy
๋ฅผ ์ํํ ๋์ memory space๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.scanf
] Try below and explain why you have an error. Fix it. char \*y;
printf("enter a string\n");
scanf("%s", y); // error becuase y has no space for a string
printf("you entered %s\n", y);
y
๊ฐ ํฌ์ธํฐ ๋ณ์์ด๊ธฐ ๋๋ฌธ์ scanf
๋ฅผ ์ํํ ๋์ memory space๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
char \* x[10];
x[0]="hi"; x[1]="bye"; x[2]="hello";
printf("%s %s %s\n", x[0],x[1],x[2]);
strcmp
, new
] Write a program that keeps reading strings and store them in a character pointer array. It stops when the user enters โendโ and displays all strings entered so far. Use new
to allocate memory and use g++ to compile.Enter a string
hi
Enter a string
aaa
Enter a string
bbb
Enter a string
end
Strings entered so far are
hi aaa bbb
char x[100];
printf("enter a sentence\n");
gets(x);
int slen=strlen(x);
printf("sentence length after gets:%d\n", slen);
for(i=0;i<slen;i++){
printf("%x ", x[i]);
}
printf("\nenter the same sentence\n");
fgets(x, 99, stdin); // read max 99 char's.
slen=strlen(x);
printf("sentence length after fgets:%d\n", slen);
for(i=0;i<slen;i++){
printf("%x ", x[i]);
}
gets()
ํจ์๋ \n(์ค๋ฐ๊ฟ๋ฌธ์)๊น์ง ๊ฐ์ ธ์ค๊ณ , \n์ \0์ผ๋ก ๋์ฒด ํ๋ค.fgets()
ํจ์๋ \n(์ค๋ฐ๊ฟ๋ฌธ์)๊น์ง ๊ฐ์ ธ์ค๊ณ , ์ถ๊ฐ์ ์ผ๋ก \0์ ๋ถ์ธ๋ค.strtok
] Use strtok
to extract words from a sentence and store them in an array. Display the number of words as below. Note that you need to copy the sentence to another string variable before doing strtok
because strtok
will destroy the original sentence.algorithm:
read a line
tokenize
display tokens
Enter a sentence
aa bcd e e ff aa bcd bcd hijk lmn al bcd
You entered aa bcd e e ff aa bcd bcd hijk lmn al bcd
There were 12 words:
aa
bcd
e
e
ff
aa
bcd
bcd
hijk
lmn
al
bcd
The original sentence was: aa bcd e e ff aa bcd bcd hijk lmn al bcd
Enter a name
kim han kook
Enter a name
park dong il
Enter a name
hong gil dong
bye
There were 3 names.
The names were
kim han kook
park dong il
hong gil dong
int x1;
char x2[12];
x1=33;
strcpy(x2,"abcdefghijkl");
printf("%d %s",x1,x2);
"abcdefghijkl"
์๋ ์จ๊ฒจ์ง ๋ฌธ์์ธ \0
์ด ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ 13์ด๋ค. ์ด๋, x2
์ ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ 12์ด๋ฏ๋ก ์๋ฌ๊ฐ ๋ฐ์ํ์๋ค.char x2[13];
์ผ๋ก ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ๋๋ ค์ค๋ค๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค.strcpy
] What is wrong with the following program? How can you fix it?int main(){
char * strarr[10]={NULL};
strarr[0]="hello";
strcpy(strarr[1],"bye");
printf("%s %s\n", strarr[0], strarr[1]);
}
strcpy(strarr[1],"bye");
์ ์คํ ํ ๋, strarr[1]
์ ํฌ๊ธฐ๊ฐ ์ ํด์ ธ ์์ง ์์ผ๋ฏ๋ก ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
new
, strcmp
] Write a program that reads a long sentence and displays the frequency of each word as below. It also prints the word that has the maximum frequency.algorithm:
read a line
tokenize
display tokens
compute frequency
display frequencies
compute max frequency word and display it
algorithm for compute frequency:
for each token
if it is already in unique_tokens[] array, increase its frequency
otherwise store in unique_tokens[] and initialize its frequency=1
Enter a sentence
aa bcd e e ff aa bcd bcd hijk lmn al bcd
You entered aa bcd e e ff aa bcd bcd hijk lmn al bcd
There were 12 words: aa bcd e e ff aa bcd bcd hijk lmn al bcd
Frequencies: aa 2 bcd 4 e 2 ff 1 hijk 1 lmm 1 al 1
The word with the max freq: bcd