Knowee
Questions
Features
Study Tools

Program to copy one string into another stringFill in the missing code in the below sample code which copies a given string into another string.Initially, read a string from the standard input device and write a loop to copy each character of given string into another string till the end of the string is reached.Place '\0' at the end of the copied string.Finally, the copied string is displayed on the screen.Click on the Live Demo button to know how to copy one string into another string by using a sample code.Sample Test CasesTest Case 1:Expected Output:Enter·a·string·:·GangaRiverThe·copied·string·=·GangaRiverTest Case 2:Expected Output:Enter·a·string·:·HyderabadThe·copied·string·=·HyderabadSubmit123456789101112#include·<stdio.h>¬void·main()·{¬————char·str1[50],·str2[50];¬————int·i;¬————printf("Enter·a·string·:·");¬————scanf("%s",·str1);¬————for·(·;·;·)··{·//Complete·the·code·in·for¬————————str2[i]·=·;¬————}¬————str2[i]·=·;·//Complete·the·statement¬————printf("The·copied·string·=·%s\n",·str2);¬}¶

Question

Program to copy one string into another stringFill in the missing code in the below sample code which copies a given string into another string.Initially, read a string from the standard input device and write a loop to copy each character of given string into another string till the end of the string is reached.Place '\0' at the end of the copied string.Finally, the copied string is displayed on the screen.Click on the Live Demo button to know how to copy one string into another string by using a sample code.Sample Test CasesTest Case 1:Expected Output:Enter·a·string·:·GangaRiverThe·copied·string·=·GangaRiverTest Case 2:Expected Output:Enter·a·string·:·HyderabadThe·copied·string·=·HyderabadSubmit123456789101112#include·<stdio.h>¬void·main()·{¬————char·str1[50],·str2[50];¬————int·i;¬————printf("Enter·a·string·:·");¬————scanf("%s",·str1);¬————for·(·;·;·)··{·//Complete·the·code·in·for¬————————str2[i]·=·;¬————}¬————str2[i]·=·;·//Complete·the·statement¬————printf("The·copied·string·=·%s\n",·str2);¬}¶

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the missing code to complete your program:

#include <stdio.h>

void main() {
    char str1[50], str2[50];
    int i;
    printf("Enter a string: ");
    scanf("%s", str1);
    for (i = 0; str1[i] != '\0'; i++) {
        str2[i] = str1[i];
    }
    str2[i] = '\0';
    printf("The copied string = %s\n", str2);
}

In the for loop, we start from the first character of the string (i = 0) and continue until we reach the null character ('\0') which marks the end of the string. For each character, we simply copy it from str1 to str2. After the loop, we add a null character at the end of str2 to mark the end of the copied string.

This problem has been solved

Similar Questions

Write a C program using string functions to perform the following functionalities: Assume: The array size as 50.To concatenate two strings.To find the length of a string.To copy one string to another.To find the size of a string.To compare two strings.Write each functionality as different function and call it from the main program.Sample InputEnter String1: VelloreEnter String2: ChennaiSample Output

Problem StatementWrite a program to remove  the characters from the first string which are present in the second stringInput FormatGiven two stringsOutput FormatPrint the updated first string

String ConcatenationWrite a program to concatenate two strings using lambda expressions. The lambda expression takes two strings as arguments and returns the concatenated string.Question instructionCreate an interface named StringConcat.Write a solution code in the main class with a lambda expression.Input format :The input consists of two strings.Output format :The output prints the concatenated string.Sample test cases :Input 1 :TakeActionOutput 1 :Take Action

Write a program to print each character of a string in incremental order.Write a program to print each character of a string in incremental order as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Keyincremental·order:·KKeKeyTest Case 2:Expected Output:str:·Pythonincremental·order:·PPyPytPythPythoPythonSubmit12345str·=·input("str:·")¬¬¬¬print("incremental·order:",···)¶

What is the output of this program if the string bar is supplied as input? #include <iostream> #include <string> using namespace std; int main() { string s1 = "foo"; string s2; getline(cin,s2); cout << s2.append(s1); return( 0 ); } bar foo foobar barfoo

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.