Algorithm:
step 1:start
step 2:declare a,b as character array
step 3:input b
step 4:print "original string",b
step 5:perform strcpy() operation
step 6:print "copied string",a
step 7:stop
Flowchart:
Code
#include <stdio.h>
int main()
{
char a[100],b[100];
printf("Enter a String:");
gets(b);
printf("Original String:%s",b);
strcpy(a,b);
printf("\nCopied String:%s",a);
return 0;
}
Enter a String:Mumbai
Original String:Mumbai
Copied String:Mumbai
Explanation of above program
In above Program we are copying one string and storing it into another using strcpy() function
strcpy()
the strcpy() function is a standard library function in C/C++.
Parameters associated with strcpy()
strcpy(destination,source)
source:source points to the variable where the string that is to be copied is stored.
destination:It points to the variable in which the string is going to be copied.
The function of strcpy() function is simply copying the content of source variable into destination variable.
Let's understand line by line
In the first line of code we have defined header file stdio.h which is used to import printf() and scanf() function to be used to learn more about header file click here we have already covered basics of C.In the second line of code we have defined main function with return type int,return type int means function will return integer value we can give any return type like char,float,int etc(other user defined
datatype created using structure and union).In the next line of code we are declaring two array of character,it will act as a string indirectly because string is nothing but an array of character.
In the next line of code,we are simply printing the message "Enter a string" after which we are taking input in string format using gets() function
gets()
this function is used to take input in string format and store the result in the given variable which is passed in gets() function.The gets() and puts() are declared in the header file stdio.h. Both the
functions are involved in the input/output operations of the strings.This function enables the user to enter some character followed by the enter key.
In the next line of code,we have written ' "Original String:"%s,b' In this Line the first part of line is within double code which will be printed as it is As we have learned that all the statement which are enclosed within double quotes will we printed as it is,after that we have put %s in printf() function,here %s is format specifier for string variable which we have mentioned immediately after it i.e b variable.So finally were are on the most import line of code i.e where the actual operation of program is getting performed.strcpy() function takes two argument the first is where the copied string should be stored and the second argument from which variable string should be copied.
In our program we have written strcpy(a,b) because we In b variable we have the stored the string that is to copied and a varible is empty string so that the when perform strcpy(a,b) the string stored in b variable will get copied into a variable.In the second last line of code we are simply printing the copied string using printf().In the last line of code we are simply returning 0 to the main function,which tell us that the program is execute successfully and did what it was intended to do.
Happy Learning :)😎
If you any have doubt,suggestion regarding this post or website then feel free to share with us.
If you have learned something new from this post then share this post with your family and
friends.
friends.
Tags:
Program
Bro plz explain this code.
ReplyDeletethe code is doing a replica of the memory in array b to array a up to the length of the string. The first character with a value 0 is considered to be the length of the string. So in this example, if the user types Mumbai, (6 characters + char 0), 7 bytes will be copied to the b array by gets(). The the strcpy will copy those 7 bytes to array a.
DeleteAs long as the input string is < 100 characters long, it will work.