Concatenating two strings means joining them to form a longer string String library functions strcat and strncat modify their string argument by adding all or part of their second argument at the end of the first argument ID: 797052
Download The PPT/PDF document "STRINGS (2) 1. strings concatenation" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
STRINGS (2)
Slide21.
strings concatenation
Concatenating two strings means joining them to form a longer string.String library functions strcat and strncat modify their string argument by adding all or part of their second argument at the end of the first argument.Both functions assume that sufficient space is allocated for the first argument to allow addition of the extra characters.
Dr. Soha S. Zaghloul
2
2
.
strings concatenation –
strcat Consider the following fragment code:
Dr. Soha S. Zaghloul
3
#define STRSIZ 17
char f1[STRSIZ] = “Mona ”; // 5 letterschar f2[STRSIZ] = “Kamelia ”; // 8 letterschar last[STRSIZ] = “Al-Shennawy”; // 11 lettersstrcat(f1, last); // f1= “Mona Al-Shennawy\0” (17 letters)strcat(f2, last); // f2= “Kamelia Al-Shennawy\0” (20 letters)
* Make sure that there is space for the null character (\0)
*
strcat
(f2, last); is not allowed because it causes an overflow.
* In order to avoid this, the
strncat
function is used.
Slide43
.
strings concatenation –
strncat Consider the following fragment code:
Dr. Soha S. Zaghloul
4
#define STRSIZ 17
char f1[STRSIZ] = “Mona ”; // 5 letterschar f2[STRSIZ] = “Kamelia ”; // 8 letterschar last[STRSIZ] = “Al-Shennawy”; // 11 lettersstrcat(f1, last); // f1= “Mona Al-Shennawy\0” (17 letters)strncat(f2, last, 7); // f2= “Kamelia Al-Shen\0” (16 letters)
*
strncat
(f2, last, 7); adds only 7 characters of last to the end of f2.
* Make sure that there is space for the null character (\0).
Slide54
.
String length –
strlen When writing a program, we usually do not know in advance the sizes of strings that will be used as data.Therefore, we use the strlen function which returns the length of a string. The null character is not counted.
Dr. Soha S. Zaghloul
5
5
.
String length –
strlen – example
Dr. Soha S. Zaghloul
6
#define STRSIZ 17
char f1[STRSIZ] = “Mona ”; // 5 letterschar f2[STRSIZ] = “Kamelia ”; // 8 letterschar last[STRSIZ] = “Al-Shennawy”; // 11 letters if (strlen(f1) + strlen(last) < STRSIZ) strcat (f1, last); // ‘\0’ is automatically placed by strcatelse {
strncat
(f1, last, STRSIZ –
strlen
(f1) – 1);
f1[STRSIZ – 1] = ‘\0’;
// add the null character to the end of f1
}
6
.
Scanning a full line
Dr. Soha S. Zaghloul
7
As previously mentioned, a space ends an input string with
scanfscanf
(“%s”, str);If the user enters in response to the above scanf:Mona Al-ShennawyThen, str = “Mona”For more interactive input, use the gets function.
Slide87
.
Scanning a full line –
gets – example
Dr. Soha S. Zaghloul
8
Consider the following example:
char line[80];printf (“Type in a line of data.\n”);gets (line); Type in a line of data.>_ Type in a line of data.>This is a short sentence
T
h
i
s
i
s
a
s
h
o
r
t
s
e
n
t
e
n
ce\00123456789101112131415161718192021222324
line
*The null character is added as the user presses the
enter
key.
* The
gets
function is in the
stdio.h
Slide98
.
Strings comparison –
strcmp
Dr. Soha S. Zaghloul
9
The
strcmp(str1, str2) function compares two strings and returns an integer as follows:RelationshipValue ReturnedExamplestr1 is less than str2negative integerstr1 is “bandary”str2 is “renad”str1 equals str2zerostr1 is “nora”str2 is “nora”str1 is greater than str2positive integer
str1
is “
sara
”
str2 is “lamia”
Slide108
.
Strings comparison –
strcmp – cont’d
Dr. Soha S. Zaghloul
10
If the first n characters of str1 and str2 are matching. str1[n] and str2[n] is the first non-matching character, then str1 is less than str2 if str1[n] < str2[n].
Example: str1 is “lama”, and str2 is “lamia”str1 is less than str2.If str1 is shorter than str2, and all characters of str1 match the corresponding characters of str2, then str1 is less than str2.Example: str1 is “nora”, str2 is “norah”str1 is less than str2.
Slide119
.
Strings comparison –
strncmp
Dr. Soha S. Zaghloul
11
The strncmp
(str1, str2, n) compares the first n characters of two strings str1 and str2.Example: str1= “manal” and str2 =“marah”, then strncmp(str1, str2, 2) returns 0.However, strncmp(str1, str2, 3) would return a negative number.
Slide1210
.
Strings comparison –
example
Dr. Soha S. Zaghloul
12
The following code fragment accepts words from a user. The process stops when the user enters a predefined sentinel.
#include <stdio.h>#define SENT “END”Int main(void){ char word[15]; printf (“Enter a word\n”); scanf (“%s”, word); while (strcmp(word, SENT) != 0) {
printf
(“Enter a word\n”);
scanf
(“%s”, word);
//process word….
}
Slide1312
.
examples
Dr. Soha S. Zaghloul
13
What is the value of t1 after the execution of these statements if the value of t2 is
“Good luck”?strncpy (t1, &t2[1], 5);
t1[5] = ‘\0’;Write a statement that assigns to s1 the end of the string value of s2 starting from the fourth character.
Slide1413
.
self-check exercises
Dr. Soha S. Zaghloul
14
Given the following declarations:
char s5[5], s10[10], s20[20];
char
aday
[7] = “
sunday
”;
char another[9] = “
saturday
”;
What is the output of the following:
strncopy
(s5, another, 4); s5[4] = ‘\0’;
strcpy
(s10, &
aday
[3]);
strlen
(another);
strcpy
(s20,
aday
);
strcat
(s20, another);