Top Banner
String Processing
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 5 2. string processing

String Processing

Page 2: 5 2. string processing

2

Preview

String

– String is a simple array of chars (characters)

– String is one-dimensional array of char

– The null character ‘\0’ must be included at the end of String

– Various built-in functions are provided for String function

Page 3: 5 2. string processing

3

The End-of-String Sentinel ‘\0’

How to define String

– 3 letters are stored in 3 byte but the null character need one

extra byte i.e., 4 bytes.

char word[100];

word[0] = ‘a’;

word[1] = ‘b’;

word[2] = ‘c’;

word[3] = ‘\0’; /* Insert null char at the end of a string*/

Page 4: 5 2. string processing

4

Initialization of Strings

Use array name

– Use char array for processing string

[Ex] char word[4] = “abc”;

[Ex] char word[4] = {‘a’, ‘b’, ‘c’, ‘\0’ };

[Ex] char word[] = {‘a’, ‘b’, ‘c’, ‘\0’ };

[Ex] char word[] = “abc”; 4 chars are automatically generated by the compiler

Page 5: 5 2. string processing

5

Displaying String and characters

printf()

– Use %s for a string output

– Return the number of the printed string if the

output is successful, if not return -1

[Ex] int nchars;

char p[ ] = “Hello! the world”;

nchars = printf(“%s”, p);

printf(“\nnum of chars=%d\n”, nchars);

Hello! the world num of chars = 16

Page 6: 5 2. string processing

6

Displaying String and characters

puts()

– Faster and simpler than printf()

– After printing a string, automatically move to the 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!!

Page 7: 5 2. string processing

7

Reading Strings from the KB

scanf()

– %s : Read until next whitespace (blank) character

– %ns : Read n characters, provided that it reads until the

whitespace as the space is found

int scanf(char *format, argument_list);

return

- no. of successfully matched and input items

- 0 if not

Page 8: 5 2. string processing

8

Reading Strings from the KB

[Ex] char name[80];

scanf(“%s”, name); /* name <- SKKU */

Input: SKKU Univ.

[Ex] char name[80];

scanf(“%3s”, name); /* name <= C-P */

scanf(“%8s”, name); /* name <= rogram */

Input: C-Program is

Read until a white space

Read 3 characters

Page 9: 5 2. string processing

9

Reading Strings from the KB

gets()

– Read string from Keyboard (KB) until ‘\n’ is entered

– ‘\n’ is automatically converted into ‘\0’ at the end of string

char* gets(char *format);

return

- the address of the string

- NULL if EOF (end-of-file)

As string is entered through scanf() :

• It skips leading whitespace characters

(It’s impossible to read whitespace.)

• It cannot enter ‘\n’ in string.

Page 10: 5 2. string processing

10

Reading Strings from the KB

[Ex] char data[81];

while( gets(data) != NULL) {

printf(“%s\n”, data);

}

or while(gets(data) != 0)

Printing Program with many lines on the screen until ALT+D is entered

Exit as <blank line> or <[ctrl] + D> are inputted

Page 11: 5 2. string processing

As you enter more characters than the size of an arrary…

– What is output value as “abcde” is entered?

11

Reading Strings from the KB

char a[4], b[4]=“1234”;

scanf(“%s”, a);

printf( “%s %s\n”, a, b ) ;

Page 12: 5 2. string processing

12

String-Handling Functions

String Assign Function

[Ex] char str1[10] = “abc”, str2[10];

str1 = “123” ;

str2 = str1 ;

OK?? Why not??

Page 13: 5 2. string processing

13

String-Handling Functions

char *strcpy(char *s1, const char *s2);

– Copy s1 string into s2 string

– This function returns a pointer to the string s1.

– s1 must be appropriately allocated for storing the string.

[Ex] char str1[10] = “abc”, str2[10];

strcpy( str1, “abc” ) ;

strcpy( str2, str1 ) ;

Page 14: 5 2. string processing

14

String-Handling Functions

String Comparison Function

[Ex] char str1[10], str2[10];

scanf( “%s”, str1 ) ;

scanf( “%s”, str2 ) ;

if( str1 == str2 ) printf( “Same!!\n” ) ;

OK?? Why not??

Page 15: 5 2. string processing

15

String-Handling Functions

int strcmp(const char *s1, const char *s2);

– Compares s1 string to s2 string

– return value < 0 : if s1 is less than s2 ASCII

– return value = 0 : if s1 and s2 are equal

– return value > 0 : if s1 is greater than s2 ASCII

[Ex] char str1[10], str2[10];

scanf( “%s”, str1 ) ;

scanf( “%s”, str2 ) ;

if( strcmp(str1,str2) == 0 ) printf( “Same!!\n” ) ;

Page 16: 5 2. string processing

16

String-Handling Functions

String Length

– How many letters do string str1 have?

[Ex] char str1[10] ;

scanf( “%s”, str1 ) ;

[Ex] char str1[10] ;

int length ;

scanf( “%s”, str1 ) ;

for( length = 0 ; s[length] != NULL ; length++ ) ;

printf( “The length of string: %d\n”, length ) ;

Page 17: 5 2. string processing

17

String-Handling Functions

int strlen(const char *s1);

– Returns a length of the string

[Ex] char str1[10] ;

int length ;

scanf( “%s”, str1 ) ;

printf( “The length of string: %d\n”, strlen(str1) ) ;

Page 18: 5 2. string processing

18

String-Handling Functions

Other String Functions

– strcat : Appends the string s2 to the end of string s1

– strchr : Searches for the first occurrence of c1 in s1

– strstr : Finds the first occurrence of the entire s2 in s1

Page 19: 5 2. string processing

19

String-Handling Functions

char *strcat(char *s1, const char *s2); – Concatenate string s2 onto the end of string s1

– Return s1

– String s1 must be appropriately allocated for storing the

string.

char str1[10]="1234";

char str2[10]="abcd";

strcat(str1, str2);

printf(“%s, %s\n", str1, str2);

strcat(str2, “efgh” ) ;

printf(“%s\n", str2);

Page 20: 5 2. string processing

20

String-Handling Functions

char* strchr(const char *s1, char c1); – Searches for the first occurrence of c1 in s1

– If c1 does not match, NULL pointer is returned.

[Ex] char str[10] ;

scanf( “%s”, str ) ;

if( strchr(str, ‘e’ ) != NULL )

printf( “e is found\n” );

else

printf( “e is not found\n” ) ;

Page 21: 5 2. string processing

21

String-Handling Functions

char* strstr(const char *s1, char* s2); – Finds the first occurrence of the entire s2 in s1

– If s1 does not match, NULL pointer is returned.

[Ex] char str[10] ;

scanf( “%s”, str ) ;

if( strchr(str, “” ) != NULL )

printf( “hi is found\n” );

else

printf( “hi is not found\n” ) ;