Unit 3 String Excercise 3
Unit 3 String Excercise 3
UNIT III
String: Exercise:-3
In this example, you will learn to concatenate two strings manually without
using the strcat() function.
As you know, the best way to concatenate two strings in C programming is
by using the strcat() function. However, in this example, we will
concatenate two strings manually.
#include <stdio.h>
int main() {
char s1[100] = "programming ", s2[] = "is awesome";
int length, j;
Continue…
// concatenate s2 to s1
for (j = 0; s2[j] != '\0'; ++j, ++length) {
s1[length] = s2[j];
}
Continue…
return 0;
}
Output
After concatenation: programming is awesome
Here, two strings s1 and s2 and concatenated and the result is stored
in s1.
In this example, you will learn to copy strings without using the strcpy()
function.
As you know, the best way to copy a string is by using the strcpy()
function. However, in this example, we will copy a string manually
without using the strcpy() function.
#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Output
Length of the string: 18
References
http://kirste.userpage.fu-berlin.de/chemnet/use/info/libc/libc_7.html
Let Us C by Yashavant Kanetkar : Authentic Guide to C PROGRAMMING Language 17th
Edition, BPB Publications
C in Depth by by S.K.Srivastava and Deepali Srivastava, BPB Publications