Answer to Question #201493 in C for Muhammad Usman

Question #201493

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!”


1
Expert's answer
2021-06-01T03:01:06-0400
#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();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS