Lecture 11
Lecture 11
Lecture 11
Character Strings
1
Topics
• Strings
– Representation
– Declaration
– Functions
– Common mistakes
– Index of a char in a string
2
Introduction
• Grouping of characters is called a string
• As C implements the strings using arrays of
type char, we could not explore string
until we had dealt with arrays
• Strings are important, because we also have
to manipulate textual data as well as
numerical data
3
On the Nature of Strings
• Recall: Main memory
– contiguous array of cells
– each cell has an address
4
On the Nature of Strings (cont.)
• Recall: Variable declaration
– sets aside a “box” to contain a value
Specifies number
of cells in the array
6
On the Nature of Strings (cont.)
• String declaration
– sets aside an array of cells
– each cell contains a char
– address of first cell in the array
Example: char name[5];
name
is 0x2000
0x2000 0x2004
7
Character Strings
Declaration 1:
char name[5];
Declaration 2:
#define MAXLENGTH 5
char name[MAXLENGTH];
name
is 0x2000
0x2000 0x2004
8
String Input/Output
#include <stdio.h>
No ampersand (&)!
#define MAXLENGTH 15
int main()
{
char string1[MAXLENGTH];
char string2[MAXLENGTH];
return 0;
}
9
String Input/Output (Alternate
functions)
#include <stdio.h>
#define MAXLENGTH 15
int main()
{
char string1[MAXLENGTH];
char string2[MAXLENGTH];
gets(string1);
gets(string2);
puts(string1);
puts(string2);
return 0; 10
}
Character Strings
Terminating Character:
Initialization: • Marks the end of string
• Special char: ’\0’
char name[5] = “Ali”;
• aka NUL (single L)
name A l i \0
is 0x2000
0x2000 0x2004
11
Character Strings
Can store
at most 4 letters,
Initialization:
because of `\0‟
char name[5] = “Ali”;
name A l i \0
is 0x2000
0x2000 0x2004
12
Character Strings
Takes up an
Declaration 3:
extra cell for „\0‟
char name[] = “Ali”;
name A l i \0
is 0x2000
0x2000 0x2003
13
Character Strings
Result is
Declaration 4: “undefined”
char *name = “Ali”; if you try to
modify this
string.
0x3000 A l i \0
name
0x3000 0x3003 14
Character Strings
Declaration 5:
char name[];
15
Arrays of Strings
• One string is an array of characters, so an
array of strings is a two-dimensional array
of characters in which each row is a string
• Example
– char days[7][10] = {“Monday”,
“Tuesday”, “Wednesday”,
“Thursday”, “Friday”,
“Saturday”, “Sunday”};
16
A Char in a String
17
A Char in a String
0x3995 0x399C
name A i s e \0
is 0x3995
index 0 index 4
name A i s e \0
is 0x3995
index 2
name[2] = „X‟;
printf(“Name: %s\n”, name);
19
A Char in a String
0x3995 0x399C
name A i X e \0
is 0x3995
index 2
name[2] = „X‟;
printf(“Name: %s\n”, name);
• #include <string.h>
• Operations:
– Assignment: strcpy()
– Concatenation: strcat()
– Comparison: strcmp()
– Length: strlen()
21
String Operation: Assignment
#include <stdio.h>
#include <string.h>
int main()
{
char string1[MAXLENGTH];
char string2[MAXLENGTH];
return 0;
}
string1: <garbage>
string2: <garbage> 22
String Operation: Assignment
#include <stdio.h>
#include <string.h>
int main()
{
char string1[MAXLENGTH];
char string2[MAXLENGTH];
return 0;
}
string1: “Hello World!”
string2: <garbage> 23
String Operation: Assignment
#include <stdio.h>
#include <string.h>
int main()
{
char string1[MAXLENGTH];
char string2[MAXLENGTH];
return 0;
}
string1: “Hello World!”
string2: “Hello World!” 24
Common Mistake:
Wrong Assignment
Example 1:
char name1[5] = “Ali”;
char name2[5] = “Sami”;
name2 = name1;
25
Common Mistake:
Bad Assignment
Example 2:
char *name1 = “Ali”;
char *name2 = “Sami”;
name2 = name1;
Better avoid
(Usually, no error message.) initialising
strings this way.
26
Common Mistake:
Bad Assignment
char *name1 = “Ali”;
char *name2 = “Sami”;
0x2000 A l i \0
name1
0x2000 0x2003
0x3990 S a m i \0
name2
0x3990 0x3994 27
Common Mistake:
Bad Assignment
name2 = name1;
0x2000 A l i \0
name1
0x2000 0x2003
0x2000 S a m i \0
name2
0x3990 0x3994 28
Common Mistake:
Not enough space
strcpy(name, “Samir”);
name A l i \0
is 0x2000
0x2000 0x2003 29
Common Mistake:
Not enough space
Requires caution.
char name[] = “Ali”;
strcpy(name, “Samir”);
name S a m i r \0
is 0x2000
0x2000 0x2003 30
String Operation: Concatenation
char string1[MAXLENGTH];
char string2[MAXLENGTH];
strcpy(string1, “Goodbye”);
strcpy(string2, “, Cruel ”);
strcat(string1, string2);
strcat(string1, string2);
strcat(string1, “World!”);
string1: “Goodbye”
string2: “, Cruel “
31
String Operation: Concatenation
char string1[MAXLENGTH];
char string2[MAXLENGTH];
strcpy(string1, “Goodbye”);
strcpy(string2, “, Cruel ”);
strcat(string1, string2);
strcat(string1, string2);
strcat(string1, “World!”);
char string1[MAXLENGTH];
char string2[MAXLENGTH];
strcpy(string1, “Goodbye”);
strcpy(string2, “, Cruel ”);
strcat(string1, string2);
strcat(string1, string2);
strcat(string1, “World!”);
char string1[MAXLENGTH];
char string2[MAXLENGTH];
strcpy(string1, “Goodbye”);
strcpy(string2, “, Cruel ”);
strcat(string1, string2);
strcat(string1, string2);
strcat(string1, “World!”);
char name[5];
strcpy(name, “Ali”);
strcat(name, “ Osman”);
name A l i \0
is 0x2000
0x2000 0x2004 35
Common Mistake:
Not enough space
char name[5];
strcpy(name, “Ali”);
strcat(name, “ Osman”);
name A l i O s m a n \0
is 0x2000
0x2000 0x2004 36
String Operation: Comparison
strcpy(string1, “Apple”);
strcpy(string2, “Wax”);
38
Common Mistake:
Wrong Comparison
strcpy(string1, “Apple”);
strcpy(string2, “Wax”);
39
String Operation: Length
char string1[100];
strcpy(string1, “Apple”);
printf(“%d\n”, strlen(string1));
output: 5
Number of char‟s
before the `\0‟.
40
Common Mistake:
Not enough space
char name[5];
name O s m a n \0
is 0x3990
0x3990 0x3994 41
Character Strings as Parameters
• Strings as formal parameters are declared as
char* or char[]
– Examples:
void Greet ( char* name )
void Greet ( char name[] )
user
Ali
43
Example: hello3.c
#include <stdio.h>
#include <string.h> int main()
#define NAMELEN 50 {
char user[NAMELEN];
/* Print a simple greeting to
the user. */ printf("Who are you? ");
scanf("%s", user);
void Greet ( char * name ) Greet(user);
{ printf("%s\n", user);
strcat(name,"! How are you?");
} return 0;
}
name user
Ali\0
44
Example: hello3.c
#include <stdio.h>
#include <string.h> int main()
#define NAMELEN 50 {
char user[NAMELEN];
/* Print a simple greeting to
the user. */ printf("Who are you? ");
scanf("%s", user);
void Greet ( char * name ) Greet(user);
{ printf("%s\n", user);
strcat(name,"! How are you?");
} return 0;
}
name user
user
return 0;
}
47
sscanf function
• sscanf function works exactly like scanf except
that instead of taking the data values for its output
parameters from the standard input device, it
takes data from the string that is its first argument
• Example
sscanf(“85 96.2 hello”,”%d%lf%s”,&n,&val,
word);
48
Summary
• A string is a contiguous array of chars
• The string identifier is the address of the first char
in the string
• Individual chars are accessed using the
str[index] notation
• There are C library functions for copying,
concatenating and comparing and finding lengths
of strings
49