NMLT Topic 10 String
NMLT Topic 10 String
NMLT Topic 10 String
Introduction to Programming
String in C
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
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
char s[] = {‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘\0’};
char s[] = “Program”; //Automatically add ‘\0’
0 1 2 3 4 5 6
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
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
9
Copy the string
Pointer dest.
char s[100];
s = “Visual C++ 6.0”; // error
strcpy(s, “Visual C++ 6.0”); // correct
10
Create duplicate string
char *s;
s = strdup(“Visual C++ 6.0”);
11
Convert string to lower case
pointer to s.
12
Convert string to upper case
pointer to s.
13
Reverse the string
14
Compare two strings (sensitive)
< 0 if s1 < s2
== 0 if s1 == s2
>0 if s1 > s2
15
Compare two strings (insensitive)
< 0 if s1 < s2
== 0 if s1 == s2
>0 if s1 > s2
16
Concatenate two strings
17
Find the substring in the string
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