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
2
3. The End-of-String Sentinel ‘0’
How to define String
char word[100];
word[0] = ‘a’;
word[1] = ‘b’;
word[2] = ‘c’;
word[3] = ‘0’; /* Insert null char at the end of a string*/
– 3 letters are stored in 3 byte but the null character need one
extra byte i.e., 4 bytes.
3
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
4
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”;
Hello! the world
num of chars = 16
nchars = printf(“%s”, p);
printf(“nnum of chars=%dn”, nchars);
5
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!!
6
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
7
8. Reading Strings from the KB
Input: SKKU Univ.
[Ex] char name[80];
scanf(“%s”, name); /* name <- SKKU */
Input: C-Program is
[Ex] char name[80];
Read 3 characters
scanf(“%3s”, name); /* name <= C-P */
scanf(“%8s”, name); /* name <= rogram */
Read until a white space
8
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
As string is entered through scanf() :
• It skips leading whitespace characters
(It’s impossible to read whitespace.)
• It cannot enter ‘n’ in string.
char* gets(char *format);
return
- the address of the string
- NULL if EOF (end-of-file)
9
10. Reading Strings from the KB
Exit as <blank line> or
<[ctrl] + D> are inputted
[Ex] char data[81];
or while(gets(data) != 0)
while( gets(data) != NULL) {
printf(“%sn”, data);
Printing Program with many lines
} on the screen until ALT+D is entered
10
11. Reading Strings from the KB
As you enter more characters than the size of
an arrary…
– What is output value as “abcde” is entered?
char a[4], b[4]=“1234”;
scanf(“%s”, a);
printf( “%s %sn”, a, b ) ;
11
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” ) ;
15
16. String-Handling Functions
String Length
[Ex] char str1[10] ;
scanf( “%s”, str1 ) ;
– How many letters do string str1 have?
[Ex] char str1[10] ;
int length ;
scanf( “%s”, str1 ) ;
for( length = 0 ; s[length] != NULL ; length++ ) ;
printf( “The length of string: %dn”, length ) ;
16
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: %dn”, strlen(str1) ) ;
17
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
18
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, %sn", str1, str2);
strcat(str2, “efgh” ) ;
printf(“%sn", str2);
19
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 foundn” );
else
printf( “e is not foundn” ) ;
20
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 foundn” );
else
printf( “hi is not foundn” ) ;
21