Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Overview
A string is a sequence of characters. It is a kind of
data.
A literal string is a sequence of characters
enclosed by a pair of double quotes " ".
A string is stored as an array of char.
Strings are often processed using pointers.
The C standard library provides many useful string
handling functions.
2
NULL
terminator is
added
automatically
at the end of
the literal
string.
200
201
202
203
204
205
206
207
208
209
Position
Character
\0
Note
NULL
3
added manually.
int main(void)
{
char name[15] = {'C',' ','P','r','o','g','r','a','m','\0'};
char greet[] = {109, 111, 114, 110, 105, 110, 103, 0};
printf("Hello, %s, %s!\n", name, greet);
NULL terminator is
return 0;
added manually.
}
Hello, C Program, morning!
Address
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
name[i]
10
11
12
13
14
Character
\0
ASCII code
67
80
114
111
103
114
97
109
Note
32
NULL
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
name[i]
10
11
12
13
14
Character
\0
Note
NULL
Exercise
#include <stdio.h>
int main(void)
{
char name[15] = "C Program";
name[1] = '-';
// Modify the content of the array name
name[6] = 0;
printf("Hello, %s, morning!\n", name);
return 0;
}
Hello, C-Prog, morning!
11
Exercise
#include <stdio.h>
int main(void)
{
char myName[15] = "C Program";
char yourName[30];
// Whole Array Assignment!
yourName = myName;
printf("Hello, %s, morning!\n", yourName);
return 0;
}
SYNTAX ERROR!!!
12
Exercise
#include <stdio.h>
int main(void)
{
char * ptr = "C Program";
ptr[1] = '-';
// Modify the content of the literal string which is constant!
ptr[6] = 0;
printf("Hello, %s, morning!\n", ptr);
return 0;
}
CRASH!!!
13
Exercise
#include <stdio.h>
int main(void)
{
char name[15] = "C Program";
char * ptr;
ptr = name;
ptr[1] = '-';
ptr[6] = 0;
printf("Hello, %s, morning!\n", name);
printf("Bye bye, %s!\n", ptr);
return 0;
printf("%d\n", strlen(str2));
// prints 17
printf("%d\n", strlen("Hello"));
// prints 5
16
17
this-is-first-string.
s1=this-is-first-string.
this is second string.
s2=this
&
scanf("%s", s1);
printf("s1=%s\n", s1);
printf("\n");
&
scanf("%s", s2);
printf("s2=%s\n", s2);
return 0;
}
char name[20];
printf("Your name? ");
gets(name);
// we ignore the return value
printf("Hello %s!\n", name);
Your name? C Program
Hello C Program!
19
Copying strings
char * strcpy( char s1[], const char s2[] )
Copies s2 (including the NULL character) into s1
We should ensure s1 be large enough to store s2
(including the NULL character)
String s1 (i.e. the address of s1[0]) is returned
char s1[7] = "123456",
s2[7] = "ABC";
s1
'\0'
s2
'\0' ?
strcpy(s1, s2);
s1 becomes
C
'\0' 5
'\0'
// Output ABC in s1
printf("%s\n", s1);
strncpy() (Examples)
char s1[7] = "123456",
s2[7] = "ABC";
strncpy(s1, s2, 2);
// output AB3456
printf("%s\n", s1);
strncpy(s1, s2, 10);
s1
'\0'
s2
'\0' ?
'\0'
'\0' 5
'\0'
s1 becomes
A
s1 becomes
A
// output ABC
printf("%s\n", s1);
strncpy(s2, "12345", 4);
// Warning! s2 may NOT be
// NULL terminated!
s2 becomes
1
22
Concatenating strings
char * strcat( char s1[], const char s2[] )
Appends s2 to s1.
// s1 becomes "ABC123456"
// s2 becomes "123456789"
23
Comparing strings
int strcmp( const char s1[], const char s2[] )
Compares s1 and s2 character-by-character (up to the
NULL character) based on their ASCII values
Returns
Zero if both strings are equal
Comparing strings
s1
s2
"ABCD"
"ABC"
"ABC"
"AB C"
""
"123"
"abc"
"ABC"
"ABC"
"0178"
strcmp(s1,s2
)
0, -ve, or +ve?
s1
'\0'
s2
'\0' ?
s1
'\0' ?
s2
'\0' ?
s1
' ' C
s2
'\0' ?
s1
'\0' ?
s2
'\0' ?
s1
'\0' ?
s2
'\0'
?
'\0'
25
a;
atoi("1234");
atoi("
\n\n3123");
atoi("ABC10");
atoi("-1+1");
atoi("99.8e3");
//
//
//
//
//
a
a
a
a
a
becomes
becomes
becomes
becomes
becomes
1234
3123
0
-1
99
26
//
//
//
//
//
//
x
x
x
x
x
x
becomes
becomes
becomes
becomes
becomes
becomes
3.14
3.4
0.0
-0.344
1200.0
1.4
27
Remark
There are many other string handling
functions in the standard library.
Check the documentation to see if an existing
function suits your need before attempting to
write your own string handling functions.
Remember, browsing through reference
manuals is the essential technique that every
C programmer should equip with.
28
Memory Demonstration
message
67
\0
10 11 12 13 14
C
\0
cptr
cptr
30
'\0'
s2 A
'\0' ?
'\0'
s2 A
'\0' ?
1st iteration
s1
2nd iteration
s1
'\0'
s2 A
'\0' ?
=
33
34
Substring Search
using strstr()35
36