CLang Lect12
CLang Lect12
1
Store in the memory
• Remind:
– Each cell in the memory is addressed.
– Each variable declaration takes one cell to
store value.
ch = ‘B’;
‘B’ etc
ch
2
Representing a string
• A character string is a char array
• Each cell in the array contains a char
• The character string must have the terminating
character (’\0’), aka null character. The address of
the first character is the string’s address
name
= 0x2000
0x2000 0x2004
3
String declaration
Declare 1:
char name[5] = “Ann”; Terminating character
(NULL character)
name A n n \0
= 0x2000
0x2000 0x2004
Equal declare:
char name[5] = {’A’,’n’,’n’,’\0’};
4
String declaration
Declare 2: An additional character
char name[] = “Ann”; for end of string ‘\0’
name A n n \0
= 0x2000
0x2000 0x2003
Attention:
char name[] = ‘Ann’; No character for end
of string (‘\0’)
name A n n
= 0x2000
0x2000 0x2002
5
String declaration
Declaration 3:
char *name = “Ann”;
0x3000 A n n \0
name
0x3000 0x3003
Can change
reference for the
pointer name
6
Input/output a string
#include <stdio.h> Declare a
constant
#define MAXLENGTH 15
int main()
{
char str1[MAXLENGTH];
char str2[MAXLENGTH]; No operant &
here
return 0;
}
7
Character in string
0x3995 0x399C
name J o h n \0
= 0x3995
index 0 index 2
8
Program to calculating the number of
character
• Calculate the characters that are not spaces in the input string
#include <stdio.h>
int main()
{
char str[80];
int dem, i;
dem = 0; i = 0;
while ( str[i] != '\0' ) {
if ( str[i] != ' ' ) dem++;
i++;
}
printf(“So ki tu khac trang trong xau la %d", dem);
return 0;
}
9
String operations
• #include <string.h>
• Use library functions declared in <string.h>
• Assignment : strcpy()
char s1[25];
char s2[25];
strcpy(s1, “Hello”);
strcpy(s2, s1);
10
String assignment
#include <stdio.h>
#include <string.h>
int main()
{
char string1[MAXLENGTH];
char string2[MAXLENGTH];
return 0;
}
char s[4];
strcpy(s, “Hello”);
12
String operations
• Concatenation : strcat()
strcat(s1, “ Anna”);
strcat(s2, “ World”);
• Memory leak maybe occurs when
copying/concatenating strings.
• Common errors:
char name[5];
strcpy(name, “Ann”);
strcat(name, “ Smith”);
13
String concatenation
char string1[80];
char string2[80];
strcpy(string1, “Goodbye”);
strcpy(string2, “, Cruel ”);
strcat(string1, string2);
strcat(string1, string2);
strcat(string1, “World!”);
14
String operations
• Comparison : strcmp()
• strcmp returns 0 if str1 = str2
<0 if str1 < str2
>0 if str1 > str2
char str1[] = “Windows”;
char str2[] = “Unix”;
if (strcmp(str1, str2) < 0)
printf(“%s %s\n”, str1, str2);
else printf(“%s %s\n”, str2, str1);
15
Common errors
16
Strings as function parameters
• Declare as char* or char[]
void greeting (char* name)
void greeting (char name[])
• It points to the first character of the string
• Changes to the string inside the function affect the
actual string
• It is not necessary to pass the length of the string to
the function
17
Example
18
Example
• Write a function that returns the name from a full name. The full name is not
an empty string and does not have extra spaces
hoten+i+1
hoten T r a n T i e n \0
#include <stdio.h>
#include <string.h>
int main()
{
char hoten[80];
return 0;
}
20
Exercises
(i) Trim left blanks, right blanks, and redundant
blanks in a string.
(ii) Inverse a string.
(iii) Copy first name or last name in a full name string.
21
Thank you
for your
attentions!