EEE 123
Character Arrays and
Strings (1)
Prepared by Dr. Mehdi Hasan Chowdhury, EEE, CUET
Character Arrays
◼ In a character array, each element stores one
character.
e.g. char c[5];
c[0]='H'; c[1]='e'; c[2]=c[3]='l'; c[4]='o';
c[0] H
c[1] e c[0]=72; c[1]=101; c[2]=c[3]=108; c[4]=111;
c[2] l
c[3] l
c[4] o
2
Character Arrays - Initialization
◼ Like other type one-dimensional arrays, the character
array can be initialized in the same way. c[0] H
e.g. char c[5] = { 'H', 'e', 'l', 'l', 'o' }; c[1] e
c[2] l
◼ If the number of initializers is less than
c[3] l
the declared size, the remaining elements
c[4] o
are initialized to null character ('\0').
c[5] \0
e.g. char c[6] = { 'H', 'e', 'l', 'l', 'o' };
3
Character Arrays - Initialization
◼ Like other type one-dimensional arrays, the character
array can be initialized in the same way. c[0] H
e.g. char c[5] = { 'H', 'e', 'l', 'l', 'o' }; c[1] e
c[2] l
◼ If the number of initializers is more than
c[3] l
the declared size, the complier will produce
c[4] o
an error.
e.g. char c[4] = { 'H', 'e', 'l', 'l', 'o' };
Error … : Too many initializers in function ...
4
Character Arrays - Initialization
◼ Like other type one-dimensional arrays, the character
array can be initialized in the same way. c[0] H
e.g. char c[5] = { 'H', 'e', 'l', 'l', 'o' }; c[1] e
c[2] l
◼ If the number of initializers is equal to
c[3] l
the declared size, the size may be omitted.
c[4] o
e.g. char c[ ] = { 'H', 'e', 'l', 'l', 'o' };
5
Character Arrays
◼ The character array can be used as other type one-
dimensional arrays. I am happy
main()
{ char c[10] = { 'I', '', 'a', 'm', '', 'h', 'a', 'p', 'p', 'y' };
int i;
for ( i = 0; i < 10; i++ )
printf ( "%c", c[i] );
}
6
Strings
◼ A string is a character array.
char c[10] = { 'I', '', 'a', 'm', '', 'h', 'a', 'p', 'p', 'y' };
The actual length of the string is not always equal
to the size of the character array.
In fact, we often concern the actual length of a
string rather than the size of a character array.
char c[100] = { 'I', '', 'a', 'm', '', 'h', 'a', 'p', 'p', 'y' };
The size of the character array is 100, but the
actual length of the string is only 10.
7
Strings
◼ The array size is often much larger than the size of
the string stored in the array. In order to represent
the end of the string, the null character '\0' is used
as the "end-of-string" marker. c[0] B
char c[5] = { 'B', 'o ', 'y' }; c[1] o
c[2] y
c[3] is the "end-of-string"
c[3] \0
marker '\0', so the elements
before it compose the string, c[4] \0
and the length of the string is 3.
8
Strings
◼ Pay attention to the distinction of the null character
'\0', and the blank space character '', and the
figure character '0'.
The ASCII value of '\0' is 0. It can't be displayed
and acts as the "end-of-string" marker.
The ASCII value of '' is 32. It can be outputted
as a blank and occupy one place.
The ASCII value of '0' is 48. It can be outputted
as a figure.
9
Strings
◼ When a string constant is stored in memory, it also
uses the null character '\0' as the "end-of-string"
marker.
For example: When the string "Hello" is stored in
memory, it requires 6 bytes storage, and the last
byte is used to store the null character '\0'.
10
Strings - Initialization
◼ Using characters.
c[0] G
char c[5] = { 'G', 'o ', 'o', 'd' };
c[1] o
◼ Using string.
c[2] o
char c[5] = { "Good" };
c[3] d
char c[5] = "Good";
c[4] \0
char c[ ] = "Good";
Notice: The array c consists of 5 elements but
not 4 elements! This declaration is equivalent to:
char c[5] = "Good";
It differs with
char c[4] = "Good";
11
Strings - Initialization
◼ The initialization of table of strings.
char fruit[ ][7] = { "Apple", "Orange",
"Grape", "Pear", "Peach"};
fruit[0] A p p l e \0 \0
fruit[1] O r a n g e \0
fruit[2] G r a p e \0 \0
fruit[3] P e a r \0 \0 \0
fruit[4] P e a c h \0 \0
12
Input and Output – %c abcdefg abcde
◼ %c abcdef_ abcde
_
main()
{ char str[6];
int i;
for ( i = 0; i < 6; i++ )
scanf ( "%c", &str[i] );
for ( i = 0; i < 6; i++ )
printf ( "%c", str[i] );
}
13
Input and Output – %s abc str[0] ?
a
◼ %s abc_ str[1] b
?
str[2] ?
c
main() str[3] \0
?
{ char str[5]; str[4] ?
scanf ( "%s", str );
printf ( "%s", str );
}
14
1) str is the address of the
Input and Output – %s string (character array),
str[0] ? a
abc
don't use &str.
◼ %s number of thestr[1]
abc_
2) The input b?
characters should str[2]
be less ?
c
than 5.
main() str[3] \0?
3) "blank space" or "tab" or
{ char str[5]; str[4]
"newline" marks the end of?
the input string.
4) The "end-of string" marker
scanf ( "%s", str ); '\0' is stored automatically.
The string is outputted from
printf ( "%s", str ); the address str (the
} address of the string), and
stopped when the element is
the "end-of string" marker
'\0'. 15
Input and Output – %s #
◼ %s a -> H
ê
Hello#=s
e
¬
main()
7l
{ char a[ ] = { 'H', 'e', 'l', 'l', 'o' };
$l
printf ( "%s", a );
o
f
}
#
=
s
\0
"
@
16
Input and Output – %s #
◼ %s a -> H
ê
e
¬
7l
$l
o
f
\0
#
Hello =
main()
s
{ char a[ ] = "Hello";
\0
printf ( "%s", a );
"
}
@
17
Input and Output – %s #
◼ %s a -> H
ê
Hell
e
¬
main()
7l
{ char a[5] = { 'H', 'e', 'l', 'l' };
$l
printf ( "%s", a );
\0
f
}
#
=
s
\0
"
@
18
Input and Output – %s #
◼ %s a -> H
ê
e
¬
7l
$l
\0
o
f
#
Hello#=s =
main()
s
{ char a[5] = "Hello";
\0
printf ( "%s", a );
"
}
@
19
Input and Output – %s #
◼ %s ê
¬
7
$
f
Error … : Too many initializers in function ... #
=
main()
s
{ char a[4]
a[5] = "Hello";
\0
printf ( "%s", a );
"
}
@
20
Input and Output – %s #
◼ %s a -> H
ê
Hell
e
¬
main()
7l
{ char a[ ] = {'H', 'e', 'l', 'l', '\0', 'o', '!', '\0'};
$l
printf ( "%s", a );
\0
f
}
#
o
=!
\0
s
The output begins from the address
"a" and ends when it encounters the \0
first null character. "
@
21
Input and Output – %s
address: a: ffd2, n: ffd6 #
a: ê¬7$☺, n: 1 a -> 'ê'
'a'
◼ %s
Please input a string: 'b'
'¬'
abc
'7'
'c'
a: abc, n: 1
main() '$'
\0
{ char a[4]; int n = 1; n -> 1
printf ( "address: a: %x, n: %x\n", a, &n ); 0
printf ( "a: %s, n: %d\n", a, n ); '*'
printf ( "Please input a string:\n" ); 's'
scanf ( "%s", a ); 0
printf ( "a: %s, n: %d", a, n ); ']'
} '@'
22
Input and Output – %s
address: a: ffd2, n: ffd6 #
a: ê¬7$☺, n: 1 a -> 'ê'
'a'
◼ %s
Please input a string: 'b'
'¬'
abc
abcd
'7'
'c'
a: abcd,
abc, n:n:10
main() '$'
'd'
\0
{ char a[4]; int n = 1; n -> \0
1
printf ( "address: a: %x, n: %x\n", a, &n ); 0
printf ( "a: %s, n: %d\n", a, n ); '*'
printf ( "Please input a string:\n" ); 's'
scanf ( "%s", a ); 0
printf ( "a: %s, n: %d", a, n ); ']'
} '@'
23
Input and Output – %s
address: a: ffd2, n: ffd6 #
a: ê¬7$☺, n: 1 a -> 'ê'
'a'
◼ %s
Please input a string: 'b'
'¬'
abc
abcd
abcde
'7'
'c'
a: abcde,
abc, n:n:n:
abcd, 10101
main() '$'
'd'
\0
{ char a[4]; int n = 1; n -> 'e'
\0
1
printf ( "address: a: %x, n: %x\n", a, &n ); \0
0
printf ( "a: %s, n: %d\n", a, n ); '*'
printf ( "Please input a string:\n" ); 's'
scanf ( "%s", a ); 0
printf ( "a: %s, n: %d", a, n ); ']'
} '@'
24
Input and Output – %s
address: a: ffd2, n: ffd6 #
a: length
The ê¬7$☺,ofn:the1 input a -> 'ê'
'a'
◼ %s
string input
Please must abestring:
less than 'b'
'¬'
the length of the array!
abc
abcd
abcde
'7'
'c'
a: abcde,
abc, n:n:n:
abcd, 10101
main() '$'
'd'
\0
{ char a[4]; int n = 1; n -> 'e'
\0
1
printf ( "address: a: %x, n: %x\n", a, &n ); \0
0
printf ( "a: %s, n: %d\n", a, n ); '*'
printf ( "Please input a string:\n" ); 's'
scanf ( "%s", a ); 0
printf ( "a: %s, n: %d", a, n ); ']'
} '@'
25
How are you?
Input and Output – %s a=How
b=are
◼ %s c=you?
main() "blank space" or "tab" or
"newline" marks the end of
{ char a[15], b[5], c[5]; the input string.
scanf ( "%s%s%s", a, b, c );
printf ( "a=%s\nb=%s\nc=%s\n", a, b, c );
}
a H o w \0
b a r e \0
c y o u ? \0
26
Input and Output – %s char s[100], c; int k;
Question: We want to input a string "This is a string.".
Can the following statements read in this string
correctly?
(A) scanf ( "%17s", s );
(B) for ( k = 0; k < 17; k++ )
(C) k = 0;
s[k] = getchar();
while ( ( c = getchar() ) != '\n' )
s [k++] = c;
s [k] = 0;
27
28