0

I need to write a simple program (no fancy pointer stuff, no library functions. It's for educational purposes) that reads the first and second names from a user and prints them out in a single line separated by a space. I am not getting the result and I am not sure why:

# include <stdio.h>
//the program loosely simulates the behaviour of strcpy
main(){
    char fname[16], sname[16], cat[31];
    int i, j;
    printf("Please enter your first name: ");
    scanf("%s", fname);
    printf("Please enter your second name: ");
    scanf("%s", sname);

    for (i=0; fname[i] != '\0'; cat[i++] = fname[i++])
        ;

    cat[i+1] = ' '; //adds a space between the tokens

    for (j=i+1; sname[j] != '\0'; cat[j++] = sname[j++])
        ;   

    printf("The final result is:\n%s", cat);
    return 0;
}
Morteza R
  • 2,229
  • 4
  • 20
  • 31
  • You have `j=i+1`, and then use `sname[j++]` --- you're accessing `sname` out of bounds, plus some UB (see the next post) and so on. I spot 3 or so errors here, at least. – Tim Čas Feb 11 '15 at 19:49
  • 5
    `cat[i++] = fname[i++]` is probably not going to do what you expect. `cat[i] = fname[i], i++` is safer. – r3mainer Feb 11 '15 at 19:50
  • 2
    @squeamishossifrage: Or he can just put the assignment in the (currently empty) body of the `for` loop, where it belongs. – Tim Čas Feb 11 '15 at 19:52
  • 1
    @TimČas Yes, that would make more sense – r3mainer Feb 11 '15 at 19:53
  • 1
    OT: [Never use `%s` in `scanf` without a field width](http://stackoverflow.com/questions/1694036). – mafso Feb 11 '15 at 19:59

3 Answers3

1

You have several problems. First, since cat has to be big enough to hold the first two strings and a space between them, it should be declared cat[32] -- 15 characters of first name, 15 characters of surname, 1 space, and 1 trailing null byte.

You're putting the space between the words in the wrong place. The first loop left i holding the next position in cat, so it should be:

cat[i] = ' ';

Next, your array indexes in the second loop are incorrect. The positions in cat are correct, because they start from where you left off the previous loop. But you need to start from 0 in sname. So this loop should be:

for (j = i+1, k = 0; sname[k] != 0; cat[j++] = sname[k++])
    ;

Finally, after concatenating the two strings, you need to append a null byte to the result, to indicate the end.

cat[j] = 0;

Another problem is that you're incrementing i twice each time through the first loop, since you use cat[i++] = fname[i++]. Each of those i++ will increment the variable. You need to separate the assignment from the increments:

for (i=0; fname[i] != '\0'; i++) {
    cat[i] = fname[i];
}

Here's a final version of the script that works:

# include <stdio.h>
//the program loosely simulates the behaviour of strcpy

int main() {
    char fname[16], sname[16], cat[32];
    int i, j, k;
    printf("Please enter your first name: ");
    scanf("%s", fname);
    printf("Please enter your second name: ");
    scanf("%s", sname);

    for (i=0; fname[i] != '\0'; i++) {
        cat[i] = fname[i];
    }

    cat[i] = ' ';

    for (j = i+1, k = 0; sname[k] != 0; cat[j++] = sname[k++]) {
    }

    cat[j] = 0;

    printf("The final result is: %s\n", cat);
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You mean the `cat` should be declared with `32` characters, right? Because it already contains `31` cells. Your analysis seems very plausibe, however when I run your corrected version, I get a two page error and the program doesn't even compile. – Morteza R Feb 11 '15 at 20:09
  • Yeah, sorry about that. What error are you getting? – Barmar Feb 11 '15 at 20:11
  • It complains about many things and is not clear where it went wrong. It says for example: `expected declaration specifiers or '...' before 'fname'` and somewhere else it says `error: invalid initializer`. Here is the corrected code: http://paste.ofcode.org/38aK7vqBuYUMirpbbNKGpZ6 – Morteza R Feb 11 '15 at 20:17
  • Your code needs to be in the `main()` function. You can't run code outside a function in C. – Barmar Feb 11 '15 at 20:18
  • Oh crap. Sorry. Yeah, I got distracted. typo. (My original program included the main function of course) Now that I correct it, it gives back the exact same result as before!! for example if I give `John` and `Doe` as fname and sname I get some weird output like `J0h` – Morteza R Feb 11 '15 at 20:20
0

I realize you set yourself a challenge to try to learn how to do something specific (and I see you are making progress towards your goal). But I always like to keep in mind there are lots of ways to get the job done -- especially in C. You know you could just do this with printf, right?

char fname[16], sname[16];
int i, j;
printf("Please enter your first name: ");
scanf("%s", fname);
printf("Please enter your second name: ");
scanf("%s", sname);

printf("%s %s\n", fname, sname);
user590028
  • 11,364
  • 3
  • 40
  • 57
0

Watch the index you use :

cat[i++] = fname[i++] and cat[j++] = sname[j++]

try to increment 'i' and 'j' at the end of your loop :

for (i=0; fname[i] != '\0'; ++i)
    cat[i] = fname[i];
// ...
for (j=0; sname[j] != '\0';++j)
    cat[j+i+1] = sname[j];
pascx64
  • 904
  • 16
  • 31