CS 116 Lecture 14 - Short
CS 116 Lecture 14 - Short
Strings (1)
Introduction
Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Christina Class
Character Variable
A variable of a
A literal constant
character type
4
getchar() and putchar()
5
Reading and Printing a
character
return 0; return 0;
} }
6
Each character is stored as an int (ASCII code).
#include <stdio.h>
int main(void) {
char x;
return 0;
}
7
String
8
A string is a character sequence and as
literal constant included in double
quotation marks “ ”.
In some programming languages, there is an
extra data type for strings not in C!
In C, a string is implemented using an array
of characters.
9
In C programming, a string is a sequence of characters
terminated with a null character \0. For example:
10
If we define a character array of length 80,
we can store different strings in this array,
e.g., “hello”, “Computing Fundamentals”,
“Today is Monday”, etc.
In order to denote the end of a string, C
stores the string termination character ‘\0’
as last character in the array.
If we initialize a character array with a string
literal, ‘\0’ is automatically appended at the
end of the string.
11
How to declare a string
char s[5];
13
How to initialize strings?
15
Conversion specifier for strings
16
scanf() to read a string
int main()
Run #2:
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
17
important!
used in printf:
%s displays the whole string until the string
termination character.
used in scanf:
%s reads all characters (and stores them in the
array) until the next whitespace.
“Hello World” cannot be input in one string using
%s
if the input string is longer than the array it is to
be stored in, the program can crash
18
we can specify the max. length of characters to
be read between % and s
char string[21];
scanf(“%20s”, string);
we use 20 instead of 21 in order to allow for the
string termination symbol to be stored
all characters that are not read by scanf if used in
this way are buffered and will be used for the next
input
if we want to avoid this:
fflush(stdin);
after scanf empties the input buffer.
19
#include <stdio.h>
int main(void) {
char s1[20] = "Hello";
char s2[] = "CS Students!";
printf("%s %s\n", s1, s2);
return 0;
}
20
#include <stdio.h> What happen if the user
input more that 19
characters?
int main(void) {
char s1[20] = "Hello";
char s2[] = "CS Students!";
printf("%s %s\n", s1, s2);
return 0;
}
A run-time error!
21
the maximum allowed
characters?
#include <stdio.h> What happen if the user
input more that 19
characters?
int main(void) {
char s1[20] = "Hello";
char s2[] = "CS Students!";
printf("%s %s\n", s1, s2);
return 0;
}
22
String Manipulation Functions
25
string length
int stringLength(char string[])
{
int i = 0;
while (string[i] != '\0') {
++i;
}
return i;
}
26
string concatenation
void stringConcat(char result[], const char string1[],
const char string2[])
{
int i, j;
i = 0;
while(string1[i] != '\0'){
result[i] = string1[i];
i++;
}
for (j=0; string2[j] != '\0'; j++){
result[i+j] = string2[j];
}
result[i+j] = '\0';
}
27
string equality
bool equalString(const char string1[], const char
string2[])
{
int i = 0;
while ((string1[i] != '\0') &&
(string2[i] != '\0') &&
(string1[i] == string2[i]))
++i;
if (string1[i] == '\0' && string2[i] == '\0')
return true;
else
return false;
}
28
Exercise
29
Exercise - Solution
30