-1

My homework problem is to make a program putting 3 strings together using strcpy, strcat, sprintf at least once each.

I'm wondering if I can use all those three without any garbage code. I've tried using strchr to use sprintf for putting strings together, but the pointer location changed so couldn't print out the whole thing.

char str1[MAX];
char str2[MAX];
char str3[MAX];
char str4[MAX];

gets(str1);
gets(str2);
gets(str3);

strcat(str1, str2);
strchr(str1, '\0');
sprintf(str1, "%s", str3);
strcpy(str4, str1);

puts(str4);

I also want to know if there is any difference in their use between strcpy and sprintf in this case.

dtell
  • 2,488
  • 1
  • 14
  • 29
  • 1
    Welcome to Stack Overflow! [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/2173917) – Sourav Ghosh May 27 '19 at 12:56
  • What is your input? What is the output you get and what is the output you expect? BTW: `strchr(str1, '\0');` doesn't do anything. What do you expect it to do? – Jabberwocky May 27 '19 at 13:08
  • @Jabberwocky when i put "hi""hello""goodbye" i want it to print out"hihellogoodbye", but it only prints out "goodbye"strchr(str1,'\0'); is to put pointer on the end of the string so that it can print out str3 right after – 헐헐할 May 27 '19 at 13:24
  • @헐헐할 please [edit] your question and put all relevant information _there_. – Jabberwocky May 27 '19 at 13:25
  • To add on @SouravGhosh's comment, please use the size-checked versions of `strcat`, `strcpy` and `sprintf`: `strncat`, `strncpy`, `snprintf` respectively. The difference in code is minimal and never justifiable. To those curious, these are common security practices to protect against buffer overflows. More often protects against bugs than attackers. – TSG May 27 '19 at 14:46

2 Answers2

0

Lets say str1 = "ab", str2 = "cd", str3 = "ef"

strcat(str1, str2);

This will concatenate str2 onto str1, now str1 = "abcd"

strchr(str1, '\0'); // this will not do anything, it will just return pointer of '\0' in str1, which is the last chatracter.

sprintf(str1, "%s", str3);

this will print "ef" into str1, old content will be lost, I believe you wanted to do

sprintf(<pointer returned from strchr>, "%s", str3);

strcpy(str4, str1);

This will just copy str1 to str4.

puts(str4);

This will print the string str4

The problem with your code is where you are doing strchr and not collecting the return value, so that you can concatenate there. In this case strcpy and sprintf are similar, but sprintf gives you lot of formatting options, see documentation. http://www.cplusplus.com/reference/cstdio/sprintf/ Also, your MAX macro should be large enough to hold strings.

Yogesh
  • 565
  • 3
  • 21
0

This doesn't do anything: strchr(str1,'\0'). Read the documentation of strchar carefully. But you don't need strchr here anyway, you proably just want this:

...
gets(str1);
gets(str2);
gets(str3);

strcpy(str4, str1);   // copy str1 into str4
strcat(str4, str2);   // append str2 to str4
strcat(str4, str3);   // append str3 to str4

puts(str4);           // print str4

As you can see, you don't need sprintf either.

But you can do the same thing using only sprintf

...
gets(str1);
gets(str2);
gets(str3);
sprintf(str4, "%s%s%s", str1, str2, str3);
puts(str4);

but then you don't need strcpy nor strcat.

Using all strcpy, strcat and sprintf is a somewhat pointless requirement, but now you should be able to do it.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115