Write a C Program that does the following:
1. Declares a C-String called ‘m1’ and initializes it with the text “Programming is great fun!”.
2. Uses C-function puts() to print this string.
3. Asks the user to enter a string named ‘m2’ (Hint: Use gets() function for this.)
4. Concatenates the two strings and stores the result in ‘m3’.
For example, if the user enters m2 as “Not Really!”, m3 should be “Programming is great fun! Not really!”
5. Inserts the user entered array (m2) into m1 after “Programming is ...”
For the above example, the resultant String would become “Programming is Not really! great fun!”
#include <stdio.h>
#include <string.h>
int main ()
{
//1. Declares a C-String called ‘m1’ and initializes it with the text “Programming is great fun!”.
char m1[]="Programming is great fun!";
char m2[50];
char m3[100]="";
char mTemp[100]="";
char m1LeftPart[100]="";
char m1RightPart[100]="";
int i,letterCounter=0;
//2. Uses C-function puts() to print this string.
puts(m1);
//3. Asks the user to enter a string named ‘m2’ (Hint: Use gets() function for this.)
printf("Enter the string m2: ");
gets(m2);
//4. Concatenates the two strings and stores the result in ‘m3’.
strcat(m3,m1);
strcat(m3," ");
strcat(m3,m2);
//For example, if the user enters m2 as “Not Really!”, m3 should be “Programming is great fun! Not really!”
puts(m3);
//5. Inserts the user entered array (m2) into m1 after “Programming is ...”
//For the above example, the resultant String would become “Programming is Not really! great fun!”
for(i=0;i<15;i++){
m1LeftPart[i]=m1[i];
}
m1RightPart[14]='\0';
for(i=14;i<strlen(m1);i++){
m1RightPart[letterCounter]=m1[i];
letterCounter++;
}
m1RightPart[letterCounter]='\0';
strcat(m1LeftPart,m2);
strcat(m1LeftPart,m1RightPart);
strcpy(m1,m1LeftPart);
puts(m1);
getchar();
getchar();
}
Comments
Leave a comment