After just getting my head round using scanf to read from the keyboard and populate an array, using ints. I now have to do this:
1. Create an array with 8 characters using the getchar function (a word of your choice) populated by the keyboard. This will be your first array.
2. Create a second array with another 8 character word using the same function.
3. Modify the program so that the contents of the first array are transposed to the second array and likewise the second array contents are transposed to the first array.
I've done this EXACT same task for integers using scanf and int arrays, see:
If you look at that screenshot; I enter 'a' for location 0 then it skips location 1 and goes straight to location 2. It looks like it's incrementing 'counter' too many times or something... there must be something wrong with my loop but I just don't know.
I thought I had it but for some reason it's not happening. Does this code look okay to you?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
printf("Enter First Word (8 Characters): ");
while (counter < 8)
{
c = getchar();
word1[counter] = c;
counter++;
}
printf("Enter Second Word (8 Characters): ");
while (counter2 < 8)
{
b = getchar();
word2[counter2] = b;
counter2++;
}
And what would be the proper way to print the arrays once they have been populated using printf?
Taking into account that all variables were properly initialized I do not see any error except that if between inputs of the two words the Enter key was pressed then it was included as a character in the second word.
When you are entering the characters at the keyboard, are you pressing the "return/enter" key for each one? If so, the getchar() function is returning a carriage return character representing that keystroke. Your program was doing exactly what is was coded to do, which wasn't really what you had in mind. Take a close look at the example that dadabe posted, and try to work out what it is supposed to do.
Regarding using printf to print your character array:
Declare your character array one element longer than the number of characters that you need. Set the last element to zero (the null terminator).
Look up the printf function (you can use the reference available here).
The "proper way" would be to use a format string containing the %s specifier, and pass the address of your array as an argument. You can use printf to print your array without a format string, but if the user has entered format specifiers, it can lead to undesired effects.
I suggest really, you should look at my example. Normally I don't show any code, but only give ideas. In your case, looking at my code is neccessary. I had a long discussion with others, who dumped the posts of users full with their listings. And I told them, not to do this. Do you think, I would have liked, to show you my code? I made no special version for beginners. It's an example with a clear idea, and written, as a professional would write it. So you need to use your brains, to grasp it. If I've made it too difficult, let me know.
But I could say also:
What would you like more, a listing, which solves your problem, or an idea, which solves the problem?
What do you think of this idea?
Why do you do a loop until 8. Wouldn't it better to do a loop until '\n' ?
Inside this loop you can count. For counter < 8 you store, if bigger, you don't store.
And after you have done this, you could also have a look at my example.