University of Science, VNU-HCM
Faculty of Information Technology
Introduction to Programming
String in C
Lecturer: Le Ngoc Thanh
Email:
[email protected] HCM City 1
Outline
1 Definition
2 Declaration
3 Operations on strings
4 Exercises
2
Definition
• Concept
– The char type can only contain one character.
To store a string (many characters) we use an
array (one dimension) of characters.
– Character string end with character ‘\0’ (null)
String length = array size – 1
• Example
char fullname[30];// 29 characters long
char datetime[9]; // 8 characters long
3
Declaration
• Initialized as regular array
– Specific length
char s[10] = {‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘\0’};
char s[10] = “Program”; //Automatically add ‘\0’
0 1 2 3 4 5 6 7 8 9
‘P’ ‘r’ ‘o’ ‘g’ ‘ r’ ‘a’ ‘m’ ‘\0’
– Automatically determine the length
char s[] = {‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘\0’};
char s[] = “Program”; //Automatically add ‘\0’
0 1 2 3 4 5 6
‘P’ ‘r’ ‘o’ ‘g’ ‘r ’ ‘a’ ‘m’ ‘\0’
4
Ouput string
• Use the printf function with the formatter "%s".
char course[50] = “Intro to Programming”;
printf(“%s”, course); // No newline
Intro to Programming
• Use the puts function
char course[50] = “Intro to Programming”;
puts(course); //Automatically newline
printf(“%s\n”, course);
Intro to Programming
_
5
Enter string
• Using the scanf function with formatter "%s"
– Accept characters from the keyboard only until they
encounter a space character or a carriage return.
– The received string does not include space characters
and newlines.
char course[50];
printf(“Enter string: ”);
scanf(“%s”, course);
printf(“Input string is: %s”, course);
Enter string: Intro to Programming
Input string is: Intro
_
6
Enter string
• Use the fgets function or cin.getline (not use gets
function)
– Receive characters from the keyboard until they
encounter a carriage return.
– The string received is what the user entered (except for
a carriage return).
char course[50];
printf(“Enter string: ”);
fgets(course, 50, stdin);//cin.getline(course,50)
printf(“Input string is: %s”, course);
Enter string: Intro to Programming
Input string is: Intro to Programming
7
Library for manipulating string
• Library <string.h>
– strlen
– strcpy
– strdup
– strlwr/strupr
– strrev
– strcmp/stricmp
– strcat
– strstr
8
Determines the string length
size_t strlen(const char *s)
Determines the string length s.
size_t instead of unsigned (in <stddef.h>)
used to measure unsigned quantities.
String length s (does not include ending
character)
char s[] = “Visual C++ 6.0”;
int len = strlen(s); // => 14
9
Copy the string
char *strcpy(char *dest, const char *src)
Copy string src to string dest, stop when
reach to ‘\0’.
! dest must be large enough to contain src
Pointer dest.
char s[100];
s = “Visual C++ 6.0”; // error
strcpy(s, “Visual C++ 6.0”); // correct
10
Create duplicate string
char *strdup(const char *s)
Create a duplicate of a given string s. The
function will create a memory with strlen (s) + 1
bytes to hold the string s. Manually destroy this
memory when not in use.
Success: return the pointer to the memory
containing the duplicated string.
Fail: return NULL.
char *s;
s = strdup(“Visual C++ 6.0”);
11
Convert string to lower case
char *strlwr(char *s)
Convert the string s to lower case (‘A’ to ‘a’,
‘B’ to ‘b’,…, ‘Z’ to ‘z’)
pointer to s.
char s[] = “Visual C++ 6.0”;
strlwr(s);
puts(s); // visual c++ 6.0
12
Convert string to upper case
char *strupr(char *s)
Convert the string s to lower case (‘a’ to ‘A’,
‘b’ to ‘B’,…, ‘z’ to ‘Z’)
pointer to s.
char s[] = “Visual C++ 6.0”;
strupr(s);
puts(s); // VISUAL C++ 6.0
13
Reverse the string
char *strrev(char *s)
Reverses the order of the characters in the
string s (except the terminating character).
The pointer to the result string.
char s[] = “Visual C++ 6.0”;
strrev(s);
puts(s); // 0.6 ++C lausiV
14
Compare two strings (sensitive)
int strcmp(const char *s1, const char *s2)
Compares the strings s1 and s2 (case
sensitive).
< 0 if s1 < s2
== 0 if s1 == s2
>0 if s1 > s2
char s1[] = “visual C++ 6.0”;
char s2[] = “Visual C++ 6.0”;
int result = strcmp(s1, s2);//=> result > 0
15
Compare two strings (insensitive)
int stricmp(const char *s1, const char *s2)
Compares the strings s1 and s2 (case
insensitive).
< 0 if s1 < s2
== 0 if s1 == s2
>0 if s1 > s2
char s1[] = “visual c++ 6.0”;
char s2[] = “VISUAL C++ 6.0”;
int result = stricmp(s1, s2);// => result = 0
16
Concatenate two strings
char* strcat(char *dest, const char *src)
Concatenate the string src after the string
dest.
!dest must be large enough to contain result
The pointer to the concatenated string.
char s1[100] = “Visual C++”;
char s2[] = “6.0”;
strcat(s1, “ ”); // => “Visual C++ ”
strcat(s1, s2); // => “Visual C++ 6.0”
17
Find the substring in the string
char* strstr(const char *s1, const char *s2)
Find the position of the first occurrence of
s2 in s1
Success: returns the pointer to the first
occurrence of s2 in s1.
Fail: return null.
char s1[] = “Visual C++ 6.0”;
char s2[] = “C++”;
if (strstr(s1, s2) != null)
printf(“Found s2 in s1…”);
18
Exercises
• Exercise 1: read some other functions
– atoi, atol, atof : convert string to number.
– itoa, ltoa, ultoa: convert numbers to strings.
– strtok
• Exercise 2: Write a function that takes a string
and returns the corresponding string (keeping
the input string unchanged):
– Convert characters to lowercase (like strlwr).
– Convert characters to uppercase (like strupr).
– Change the first characters of each word to an
uppercase letter.
– Remove leading and trailing spaces in string.
19
Exercises
• Exercise 3: Write a function that receive a string s
and returns the corresponding string after removing
spaces.
• Exercise 4: Write a function that receive a string s
and counts how many words in that string.
• Exercise 5: Write a function that receive a string s
and outputs words on consecutive lines.
• Exercise 6: Write a function to find the word with the
largest length and output it to the screen and its
length respectively.
• Exercise 7: Write a function that extracts the first /
last n characters the given string s.
20
The End