Computer-Science

5. Review on String


Contents


1. basics

string์˜ ์ •์˜

char constant (๋ฌธ์ž ์ƒ์ˆ˜)

x = 'a';
// The compiler changes above to
x = 97; // 97 is the ascii number of 'a'

string constant (๋ฌธ์ž์—ด ์ƒ์ˆ˜)

char * x= "hello";
// The compiler changes above to (if "hello" is stored at 0x400640)
char * x = 0x400640;

string functions

Understand the difference below:

a)

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

b)

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

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

2. A string is a character array ending with 0.

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;


๋ฌธ์ž์—ด์˜ ์ถœ๋ ฅ

string์€ printf๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

printf("%s", b);


regular character array๋ฅผ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

for(i=0;i<5;i++)
   printf("%c",a[i]);

3. 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));

4. strtok

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;
}

5. char pointer array

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

6. Exercise

1) [A char constant is an ascii number. A string constant is an address where it is stored in the string area.] Explain the result for following code.

#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.

2) [A char constant is an ascii number] Try following code and explain the result.

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.

3) Try below. Compare the result with that of Problem 2).

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.

4) Declare a character array with 128 rooms. Store 0 to 127 in this array and print the corresponding character for each ascii code in the array. Find ASCII table in the Internet and confirm the results.

   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]);
   }

ASCII Table

5. [strlen] Read a string and display its length.

Enter a string
hello
The length is 5

6. [A string is a char array ending with 0] Read a string and display each character in different lines.

Enter a string
hello
h
e
l
l
o

6-1) [A string is a char array ending with 0] Try below and explain the result. Use g++ to compile.

char x[10];
strcpy(x, "hello");
strcpy(x, "hi");
for(int i=0;i<10;i++){
   printf("%d ", x[i]);
}

7. [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.

8. [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

9. [string constant] A string constant such as โ€œhelloโ€ is an address. Explain the result of following code.

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);

10. [string constant is an address] Try below and explain why we have an error.

    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

11. [You need memory space for 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"

12. [You need memory space for 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);

13. [char pointer array] Define a character pointer array and store/display strings as below.

    char \* x[10];
    x[0]="hi"; x[1]="bye"; x[2]="hello";
    printf("%s %s %s\n", x[0],x[1],x[2]);

14. [char pointer array, 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

15. [gets, fgets] Read the same sentence with gets() and fgets() and explain the difference. (Ignore warning for gets. It is a security warning because gets can cause security problem.)

   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]);
   }

16. [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

17. [char pointer array, new, strcmp] Write a program that keeps reading a name and stores it in a character pointer array until the user enters bye. The program should display all names after it sees โ€œbyeโ€.

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

18. [There is a hidden 0 at the end of a string] Try below and explain why it behaves strange. How can you fix it?

int x1;
char x2[12];
x1=33;
strcpy(x2,"abcdefghijkl");
printf("%d %s",x1,x2);

19. [You need memory space for 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]);
}

20. [char pointer array, 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