English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

what is the use of
strcpy(c," ");
strcat(c,a); in the program? why is it used? and what would have happened if it was not used?
plz help.

// Program to implement reversing the sentence
// ex an apple a day
// output : day a apple an

#include
#include
#include
#include
void main()
{
clrscr();
int l,l1,i,j,k=0;
char a[50],b[50],c[50];
cout<<"enter a sentence";
gets(a);
l=strlen(a);
strcpy(c," "); ////////////////
strcat(c,a); ///////////////
for(i=l-1;i>=0;i--)
{
if(a[i]!=' ')
{
b[k]=a[i];
k=k+1;
}
else {
b[k]='\0';
k=0;

l1=strlen(b);
for(j=l1-1;j>=0;j--)
{
cout< }
cout<<" ";
}
}
cout< cout<<"Program over.....press any key to continue";
getch();
}

i understood the program except the two lines marked there.
plz help.
just answer 3 questions--->
WHAT IS ITS USE?
WHY IS IT USED?
WHAT IF THAT WAS NOT USED?

2007-01-06 01:26:56 · 7 answers · asked by priya . 1 in Computers & Internet Programming & Design

7 answers

they were meant to preserve the original string in case a had to be altered but here it is nothing more than repetiton and is not required..........
strcpy(c,"") copies blank string to c to remove garbage values
strcat concatenates the string a to c(same as strcpy here)

2007-01-06 21:10:41 · answer #1 · answered by preeti 2 · 0 0

Hi Priya,

The strcpy(source,dest) function copies the string pointed to be source(including the terminating `\0' character) to the variable
pointed to by dest. The strings may not overlap, and the
destination string dest must be large enough to receive
the copy.
Here you have mentioned that 'strcpy(c,"")'... Where c is the source and "" is the destination.. That means c will have the value ""(i.e, EMPTY).. (This is more similar to c="")
Similarly strcat(c,a), is called as string concatenation function,
it is used basically for joining two STRINGS ONLY. That is here the output will be c+a.
Eg: c has "Hello" and a has " World". Then strcat(c,a) will give the output of "Hello World"...

2007-01-06 01:40:47 · answer #2 · answered by JamesBond999 2 · 0 0

strcpy(dest, source) copies one string to another. In this case, the literal string, "", is being copied to c[]. The reason the writer does this is to initialize the variable so that the next instruction can be performed.

strcat(dest, source) adds one string to the end of another. This would not have worked had a[] not been initialized since you can't add a string to nothing (but you can add a string to an empty string). These two lines have no effect on the output of the program. They do, however, take up memory and cpu cycles.

2007-01-06 02:07:49 · answer #3 · answered by Shawn7400 2 · 0 0

Well, those two lines basically add a space at the beginning of string a and assign it to string c.

String c is not used after that, so it is used for no reason.

If it is not used, then nothing would change (apart from saving 50 bytes of memory).

If the line had been "strcat(a,c);" instead, then it would have added a space at the end of string a and things might be a bit different.

Hope that helps.

2007-01-06 01:37:33 · answer #4 · answered by feeltherisingbuzz 4 · 0 0

well lady the 2 statements are not req in the above code...
they were meant to preserve the original string in case a had to be altered but here it is nothing more than repetiton and is not required..........
strcpy(c,"") copies blank string to c to remove garbage values
strcat concatenates the string a to c(same as strcpy here)

anyways the output i suppose will be wrong since program is does not appear correct to me! i wrote some code chk it , it works:
#include
#include
#include
#include
#include

int main(){
clrscr();
char a[50],b[50];
int i,k=0,front,end,ctr=0;

cout<<"Enter a string ; ";
gets(a);
front=end=strlen(a)-1;
strcpy(b,"");

while (isspace(a[i++])) ctr++;

for (i=strlen(a)-1;i>0;i--){
if (!isspace(a[i]))
front--;
else {
for (int j=front+1;j<=end;j++)
b[k++]=a[j];
b[k++]=' ';
end=--front;
}
}

i=front=0;

while (!front && ctr == 0){
if (isspace(a[i]))
i++;
else {
while(!isspace(a[i]))
b[k++]=a[i++];
front=1;
}
}

i=0;

while (i cout<
getch();
return 0;
}

2007-01-06 01:37:23 · answer #5 · answered by AM 3 · 0 1

strcpy(c," "); initializes the character array c to an empty string. strcat(c,a); copies the array a to the array c ( as it was previously set to an empty string, so cat appends to a null string which has the same effect as copying it. The two lines could be replaced by strcpy(c,a); If you did the strcat without the strcpy(c, "" ); the array c would not be initializes and could contain anything, and may overrun its size (50) when the strcat is done. this would cause the prrogram to crash.

2016-03-28 22:43:05 · answer #6 · answered by ? 4 · 0 0

O my god,What is that all about!!!!!

2007-01-06 01:29:22 · answer #7 · answered by Bella 7 · 0 2

fedest.com, questions and answers