I want to input:
abc def ghi jkl
and the output should be:
abc
def
ghi
jkl
I want to store each string in an array and then use a for cycle to print each position.
I have this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char vector[100];
int i = 0;
int aux = 0;
while (i < 5)
{
scanf("%s", &vector[i]);
i++;
aux+= 1;
}
for (i=0;i<aux;i++)
{
printf("%s\n", &vector[i]);
}
return 0;
}
What am I doing wrong?
Second question:
How can I change the code to stop reading my inputs when I press ctrlD and print the output?
You’re taking the address of a character in your “vector”, in stead of filling up a few strings. These modifications:
As for the ctrl-D bit, you can have it stop reading at the end of input, but you’d have to manage getting lots of input (so you’d potentially have to dynamically allocate the buffer into which you “parse” your string with
scanf)