際際滷

際際滷Share a Scribd company logo
String Processing
2
Preview
 String
 String : 朱 charactersる 蟲
 C String  char one-dimensional array襦 
 String 覦 襷 襷讌襷 null 覓語 0螳 伎 
 C String process襯   螳讌 Built-in Functions
螻
3
The End-of-String Sentinel 0
 String  覦覯
 3蠍襯 ロ讌襷, れ襦 4bytes螳 .
char word[100];
word[0] = a;
word[1] = b;
word[2] = c;
word[3] = 0; /* string 襯  null char 曙*/
4
Initialization of Strings
 Use array name
 String process襯  char array襯 
[Ex] char word[4] = abc;
[Ex] char word[4] = {a, b, c, 0 };
[Ex] char word[] = {a, b, c, 0 };
[Ex] char word[] = abc; compiler  朱
4 char襯  array
5
Displaying String and characters
 printf()
 String豢レ 伎 %s 襯 
 豢レ 炎概企 豢ル 蠍  覦, 蠏碁讌
朱 -1 覦
[Ex] int nchars;
char p[ ] = Hello! the world;
nchars = printf(%s, p);
printf(nnum of chars=%dn, nchars);
Hello! the world
num of chars = 16
6
Displaying String and characters
 puts()
 printf()覲企 fast, simple
 String 豢 , next line朱  企
int puts(char *str); /*function prototype */
return
- no. of chars written if successful
- EOF(-1) if not
[Ex] char p[ ] = Hi !!;
puts(p);
puts(Hello!!); Hi !!
Hello!!
7
Reading Strings from the KB
 scanf()
 %s : next white space char 蟾讌 read
 %ns : n螳 chars 襯 read,  蠏  white space螳 り
覃 white space蟾讌襯 read
int scanf(char *format, argument_list);
return
- no. of successfully matched and input items
- 0 if not
8
Reading Strings from the KB
[Ex] char name[80];
scanf(%s, name); /* name <- SKKU */
SKKU Univ.  
[Ex] char name[80];
scanf(%3s, name); /* name <= C-P */
scanf(%8s, name); /* name <= rogram */
C-Program is  
White space 蟾讌 read
3螳 char襯 read
9
Reading Strings from the KB
 gets()
 KB襦覿 n蟾讌, 讀 line襦 read
 n  0襦 convert string ル.
char* gets(char *format);
return
- the address of the string
- NULL if EOF (end-of-file)
scanf()襯 牛 String  覦 蟆曙:
 white space skip朱 white space read 覿螳.
 Line 襦 string  覦  .
10
Reading Strings from the KB
[Ex] char data[81], *P;
while( gets(data) != NULL) {
printf(%sn, data);
}
or while(gets(data) != 0)
^D襯 ロ 蟾讌 line襦
read 蠏碁襦 覃伎 豢
 program
<blank line>   轟
<[ctrl] + D>   譬襭
11
Reading Strings from the KB
char a[5], b[4]=1234;
scanf(%s, a);
printf( %s %sn, a, b ) ;
 Array蠍磯慨  襷 覓語襯 ロ 蟆曙
 abcde襯 ロ覃 豢 螳?
12
String-Handling Functions
 String Assign 
[Ex] char str1[10] = abc, str2[10];
str1 = 123 ;
str2 = str1 ;
OK?? Why not??
13
String-Handling Functions
 char *strcpy(char *s1, const char *s2);
 s1 s2 覓語伎 伎 覲旧
 Return 螳 s1 譯殊螳
 s1 覓語伎 ロ   豢覿 覃覈襴襯 覲危伎 
[Ex] char str1[10] = abc, str2[10];
strcpy( str1, abc ) ;
strcpy( str2, str1 ) ;
14
String-Handling Functions
 String 觜蟲 
[Ex] char str1[10], str2[10];
scanf( %s, str1 ) ;
scanf( %s, str2 ) ;
if( str1 == str2 ) printf( Same!!n ) ;
OK?? Why not??
15
String-Handling Functions
 int strcmp(const char *s1, const char *s2);
 string 蟲燕 char ascii code螳   觜蟲
 return value < 0 : s1  s2 覲企 ASCII 螳  
 return value = 0 : s1 螻 s2螳 螳 
 return value > 0 : s1  s2 覲企 ASCII 螳  
[Ex] 
#include <string.h>

char str1[10], str2[10];
scanf( %s, str1 ) ;
scanf( %s, str2 ) ;
if( strcmp(str1,str2) == 0 ) printf( Same!!n ) ;
16
String-Handling Functions
 String Length
 覈 蠍螳 str1 蟾?
[Ex] char str1[10] ;
scanf( %s, str1 ) ;
[Ex] char str1[10] ;
int length ;
scanf( %s, str1 ) ;
for( length = 0 ; str[length] != NULL ; length++ ) ;
printf( The length of string: %dn, length ) ;
17
String-Handling Functions
 int strlen(const char *s1);
 String 蠍語企ゼ return
[Ex] char str1[10] ;
int length ;
scanf( %s, str1 ) ;
printf( The length of string: %dn, strlen(str1) ) ;
18
String-Handling Functions
 蠏  String 
 strcat :  string るジ string  覲旧
 strchr : string 伎 譯殊伎 覓語 谿剰鍵
 strstr : string 伎 譯殊伎 string 谿剰鍵
19
String-Handling Functions
 char *strcat(char *s1, const char *s2);
 s1 覓語 れ s2 覓語伎 豢螳
 Return螳 s1
 s1 覓語伎 ロ   豢覿 覃覈襴襯 覲危伎 
char str1[10]="1234";
char str2[10]="abcd";
strcat(str1, str2);
printf(%s, %sn", str1, str2);
strcat(str2, efgh ) ;
printf(%sn", str2);
20
String-Handling Functions
 char* strchr(const char *s1, char c1);
 s1 覓語伎 螳 襾殊  覓語 c1 pointer襯
return
 c1 企 覓語螳  蟆曙 null pointer襯 return
[Ex] char str[10] ;
scanf( %s, str ) ;
if( strchr(str, e ) != NULL )
printf( e is foundn );
else
printf( e is not foundn ) ;
21
String-Handling Functions
 char* strstr(const char *s1, char* s2);
 strchr螻 讌襷 覓語 sub-String search
  蟆曙 null pointer襯 return
[Ex] char str[10] ;
scanf( %s, str ) ;
if( strchr(str,  ) != NULL )
printf( hi is foundn );
else
printf( hi is not foundn ) ;
Ad

Recommended

[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)
Bill Kim
Data Structure - 1st Study
Data Structure - 1st Study
Chris Ohk
RNC C++ lecture_5 Array
RNC C++ lecture_5 Array
itlockit
MD5 願蟆
MD5 願蟆
EG Lim
Functional programming
Functional programming
ssuserdcfefa
襦蠏碁覦 : C++11 伎手鍵
襦蠏碁覦 : C++11 伎手鍵
Jongwook Choi
[devil's camp] - 螻襴讀 STL (覦語)
[devil's camp] - 螻襴讀 STL (覦語)
NAVER D2
5 1. character processing
5 1. character processing
[TechDays Korea 2015] 轟 C++ 貊 覈 C++襦 蠍磯豺蠍
[TechDays Korea 2015] 轟 C++ 貊 覈 C++襦 蠍磯豺蠍
Chris Ohk
蟆 覓殊 企
蟆 覓殊 企
Windosw via c ろ磯2
Windosw via c ろ磯2
HolyTak
Javascript hoisting
Javascript hoisting
Ohgyun Ahn
Ch10
Ch10
Hankyo
[覦″旧 貉危郁骸螻] C 襦蠏碁覦 螻殊覓
[覦″旧 貉危郁骸螻] C 襦蠏碁覦 螻殊覓
Lee Sang-Ho
NDC 2014, 狩 覓語伎 瑚
NDC 2014, 狩 覓語伎 瑚
tcaesvk
2 1. variables & data types
2 1. variables & data types
14. fiile io
14. fiile io
遺襭
遺襭
koominsu
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
伎 覓語 危危蠍
伎 覓語 危危蠍
Yong Joon Moon
=求メ =п 求≡=
=求メ =п 求≡=
Yong Joon Moon
Ch.14 螳c v0.6
Ch.14 螳c v0.6
麹 蟾
G+ Summer C Study 20130703(1殊姶)
G+ Summer C Study 20130703(1殊姶)
Jake Yoon
2. 界庚語伎 蠍磯蓋
2. 界庚語伎 蠍磯蓋
SeonMan Kim
襭蟲譟(data structure)_NOTE 4. 梶≡(stack).pdf
襭蟲譟(data structure)_NOTE 4. 梶≡(stack).pdf
shoelaces122
G+ Summer C Study 20130711(4殊姶)
G+ Summer C Study 20130711(4殊姶)
Jake Yoon
11. array & pointer
11. array & pointer

More Related Content

What's hot (6)

5 1. character processing
5 1. character processing
[TechDays Korea 2015] 轟 C++ 貊 覈 C++襦 蠍磯豺蠍
[TechDays Korea 2015] 轟 C++ 貊 覈 C++襦 蠍磯豺蠍
Chris Ohk
蟆 覓殊 企
蟆 覓殊 企
Windosw via c ろ磯2
Windosw via c ろ磯2
HolyTak
Javascript hoisting
Javascript hoisting
Ohgyun Ahn
Ch10
Ch10
Hankyo
5 1. character processing
5 1. character processing
[TechDays Korea 2015] 轟 C++ 貊 覈 C++襦 蠍磯豺蠍
[TechDays Korea 2015] 轟 C++ 貊 覈 C++襦 蠍磯豺蠍
Chris Ohk
蟆 覓殊 企
蟆 覓殊 企
Windosw via c ろ磯2
Windosw via c ろ磯2
HolyTak
Javascript hoisting
Javascript hoisting
Ohgyun Ahn
Ch10
Ch10
Hankyo

Similar to 5 2. string processing (20)

[覦″旧 貉危郁骸螻] C 襦蠏碁覦 螻殊覓
[覦″旧 貉危郁骸螻] C 襦蠏碁覦 螻殊覓
Lee Sang-Ho
NDC 2014, 狩 覓語伎 瑚
NDC 2014, 狩 覓語伎 瑚
tcaesvk
2 1. variables & data types
2 1. variables & data types
14. fiile io
14. fiile io
遺襭
遺襭
koominsu
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
伎 覓語 危危蠍
伎 覓語 危危蠍
Yong Joon Moon
=求メ =п 求≡=
=求メ =п 求≡=
Yong Joon Moon
Ch.14 螳c v0.6
Ch.14 螳c v0.6
麹 蟾
G+ Summer C Study 20130703(1殊姶)
G+ Summer C Study 20130703(1殊姶)
Jake Yoon
2. 界庚語伎 蠍磯蓋
2. 界庚語伎 蠍磯蓋
SeonMan Kim
襭蟲譟(data structure)_NOTE 4. 梶≡(stack).pdf
襭蟲譟(data structure)_NOTE 4. 梶≡(stack).pdf
shoelaces122
G+ Summer C Study 20130711(4殊姶)
G+ Summer C Study 20130711(4殊姶)
Jake Yoon
11. array & pointer
11. array & pointer
覓語 伎
覓語 伎
SangJaeLee_90
C語 譬蟆 碁碁 1
C語 譬蟆 碁碁 1
Jong Hyuck Lim
=п求= メ罪求
=п求= メ罪求
Seungyong Lee
G+ Summer C Study 20130709(3殊姶)
G+ Summer C Study 20130709(3殊姶)
Jake Yoon
[覦″旧 貉危郁骸螻] C 襦蠏碁覦 螻殊覓
[覦″旧 貉危郁骸螻] C 襦蠏碁覦 螻殊覓
Lee Sang-Ho
NDC 2014, 狩 覓語伎 瑚
NDC 2014, 狩 覓語伎 瑚
tcaesvk
2 1. variables & data types
2 1. variables & data types
14. fiile io
14. fiile io
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
Part14 %ed%8 c%8c%ec%9d%bc%ec%9e%85%ec%b6%9c%eb%a0%a5
伎 覓語 危危蠍
伎 覓語 危危蠍
Yong Joon Moon
=求メ =п 求≡=
=求メ =п 求≡=
Yong Joon Moon
Ch.14 螳c v0.6
Ch.14 螳c v0.6
麹 蟾
G+ Summer C Study 20130703(1殊姶)
G+ Summer C Study 20130703(1殊姶)
Jake Yoon
2. 界庚語伎 蠍磯蓋
2. 界庚語伎 蠍磯蓋
SeonMan Kim
襭蟲譟(data structure)_NOTE 4. 梶≡(stack).pdf
襭蟲譟(data structure)_NOTE 4. 梶≡(stack).pdf
shoelaces122
G+ Summer C Study 20130711(4殊姶)
G+ Summer C Study 20130711(4殊姶)
Jake Yoon
11. array & pointer
11. array & pointer
C語 譬蟆 碁碁 1
C語 譬蟆 碁碁 1
Jong Hyuck Lim
=п求= メ罪求
=п求= メ罪求
Seungyong Lee
G+ Summer C Study 20130709(3殊姶)
G+ Summer C Study 20130709(3殊姶)
Jake Yoon
Ad

More from (20)

15 3. modulization
15 3. modulization
15 2. arguement passing to main
15 2. arguement passing to main
13. structure
13. structure
12 2. dynamic allocation
12 2. dynamic allocation
12 1. multi-dimensional array
12 1. multi-dimensional array
10. pointer & function
10. pointer & function
9. pointer
9. pointer
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
6. function
6. function
15 1. enumeration, typedef
15 1. enumeration, typedef
4. loop
4. loop
3 2. if statement
3 2. if statement
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
2 3. standard io
2 3. standard io
2 2. operators
2 2. operators
Goorm ide 蟲′覯 for skku()
Goorm ide 蟲′覯 for skku()
蟲襴 蠍磯蓋 螳襭
蟲襴 蠍磯蓋 螳襭
Goorm ide 螳 殊企(蟲′ 覯)
Goorm ide 螳 殊企(蟲′ 覯)
W14 chap13
W14 chap13
13th chapter12 slide
13th chapter12 slide
15 3. modulization
15 3. modulization
15 2. arguement passing to main
15 2. arguement passing to main
13. structure
13. structure
12 2. dynamic allocation
12 2. dynamic allocation
12 1. multi-dimensional array
12 1. multi-dimensional array
10. pointer & function
10. pointer & function
9. pointer
9. pointer
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
6. function
6. function
15 1. enumeration, typedef
15 1. enumeration, typedef
4. loop
4. loop
3 2. if statement
3 2. if statement
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
2 3. standard io
2 3. standard io
2 2. operators
2 2. operators
Goorm ide 蟲′覯 for skku()
Goorm ide 蟲′覯 for skku()
蟲襴 蠍磯蓋 螳襭
蟲襴 蠍磯蓋 螳襭
Goorm ide 螳 殊企(蟲′ 覯)
Goorm ide 螳 殊企(蟲′ 覯)
W14 chap13
W14 chap13
13th chapter12 slide
13th chapter12 slide
Ad

5 2. string processing

  • 2. 2 Preview String String : 朱 charactersる 蟲 C String char one-dimensional array襦 String 覦 襷 襷讌襷 null 覓語 0螳 伎 C String process襯 螳讌 Built-in Functions 螻
  • 3. 3 The End-of-String Sentinel 0 String 覦覯 3蠍襯 ロ讌襷, れ襦 4bytes螳 . char word[100]; word[0] = a; word[1] = b; word[2] = c; word[3] = 0; /* string 襯 null char 曙*/
  • 4. 4 Initialization of Strings Use array name String process襯 char array襯 [Ex] char word[4] = abc; [Ex] char word[4] = {a, b, c, 0 }; [Ex] char word[] = {a, b, c, 0 }; [Ex] char word[] = abc; compiler 朱 4 char襯 array
  • 5. 5 Displaying String and characters printf() String豢レ 伎 %s 襯 豢レ 炎概企 豢ル 蠍 覦, 蠏碁讌 朱 -1 覦 [Ex] int nchars; char p[ ] = Hello! the world; nchars = printf(%s, p); printf(nnum of chars=%dn, nchars); Hello! the world num of chars = 16
  • 6. 6 Displaying String and characters puts() printf()覲企 fast, simple String 豢 , next line朱 企 int puts(char *str); /*function prototype */ return - no. of chars written if successful - EOF(-1) if not [Ex] char p[ ] = Hi !!; puts(p); puts(Hello!!); Hi !! Hello!!
  • 7. 7 Reading Strings from the KB scanf() %s : next white space char 蟾讌 read %ns : n螳 chars 襯 read, 蠏 white space螳 り 覃 white space蟾讌襯 read int scanf(char *format, argument_list); return - no. of successfully matched and input items - 0 if not
  • 8. 8 Reading Strings from the KB [Ex] char name[80]; scanf(%s, name); /* name <- SKKU */ SKKU Univ. [Ex] char name[80]; scanf(%3s, name); /* name <= C-P */ scanf(%8s, name); /* name <= rogram */ C-Program is White space 蟾讌 read 3螳 char襯 read
  • 9. 9 Reading Strings from the KB gets() KB襦覿 n蟾讌, 讀 line襦 read n 0襦 convert string ル. char* gets(char *format); return - the address of the string - NULL if EOF (end-of-file) scanf()襯 牛 String 覦 蟆曙: white space skip朱 white space read 覿螳. Line 襦 string 覦 .
  • 10. 10 Reading Strings from the KB [Ex] char data[81], *P; while( gets(data) != NULL) { printf(%sn, data); } or while(gets(data) != 0) ^D襯 ロ 蟾讌 line襦 read 蠏碁襦 覃伎 豢 program <blank line> 轟 <[ctrl] + D> 譬襭
  • 11. 11 Reading Strings from the KB char a[5], b[4]=1234; scanf(%s, a); printf( %s %sn, a, b ) ; Array蠍磯慨 襷 覓語襯 ロ 蟆曙 abcde襯 ロ覃 豢 螳?
  • 12. 12 String-Handling Functions String Assign [Ex] char str1[10] = abc, str2[10]; str1 = 123 ; str2 = str1 ; OK?? Why not??
  • 13. 13 String-Handling Functions char *strcpy(char *s1, const char *s2); s1 s2 覓語伎 伎 覲旧 Return 螳 s1 譯殊螳 s1 覓語伎 ロ 豢覿 覃覈襴襯 覲危伎 [Ex] char str1[10] = abc, str2[10]; strcpy( str1, abc ) ; strcpy( str2, str1 ) ;
  • 14. 14 String-Handling Functions String 觜蟲 [Ex] char str1[10], str2[10]; scanf( %s, str1 ) ; scanf( %s, str2 ) ; if( str1 == str2 ) printf( Same!!n ) ; OK?? Why not??
  • 15. 15 String-Handling Functions int strcmp(const char *s1, const char *s2); string 蟲燕 char ascii code螳 觜蟲 return value < 0 : s1 s2 覲企 ASCII 螳 return value = 0 : s1 螻 s2螳 螳 return value > 0 : s1 s2 覲企 ASCII 螳 [Ex] #include <string.h> char str1[10], str2[10]; scanf( %s, str1 ) ; scanf( %s, str2 ) ; if( strcmp(str1,str2) == 0 ) printf( Same!!n ) ;
  • 16. 16 String-Handling Functions String Length 覈 蠍螳 str1 蟾? [Ex] char str1[10] ; scanf( %s, str1 ) ; [Ex] char str1[10] ; int length ; scanf( %s, str1 ) ; for( length = 0 ; str[length] != NULL ; length++ ) ; printf( The length of string: %dn, length ) ;
  • 17. 17 String-Handling Functions int strlen(const char *s1); String 蠍語企ゼ return [Ex] char str1[10] ; int length ; scanf( %s, str1 ) ; printf( The length of string: %dn, strlen(str1) ) ;
  • 18. 18 String-Handling Functions 蠏 String strcat : string るジ string 覲旧 strchr : string 伎 譯殊伎 覓語 谿剰鍵 strstr : string 伎 譯殊伎 string 谿剰鍵
  • 19. 19 String-Handling Functions char *strcat(char *s1, const char *s2); s1 覓語 れ s2 覓語伎 豢螳 Return螳 s1 s1 覓語伎 ロ 豢覿 覃覈襴襯 覲危伎 char str1[10]="1234"; char str2[10]="abcd"; strcat(str1, str2); printf(%s, %sn", str1, str2); strcat(str2, efgh ) ; printf(%sn", str2);
  • 20. 20 String-Handling Functions char* strchr(const char *s1, char c1); s1 覓語伎 螳 襾殊 覓語 c1 pointer襯 return c1 企 覓語螳 蟆曙 null pointer襯 return [Ex] char str[10] ; scanf( %s, str ) ; if( strchr(str, e ) != NULL ) printf( e is foundn ); else printf( e is not foundn ) ;
  • 21. 21 String-Handling Functions char* strstr(const char *s1, char* s2); strchr螻 讌襷 覓語 sub-String search 蟆曙 null pointer襯 return [Ex] char str[10] ; scanf( %s, str ) ; if( strchr(str, ) != NULL ) printf( hi is foundn ); else printf( hi is not foundn ) ;