In: Computer Science
When you read data in (without using getLine) how is the data read. does it read spaces and line breakers(\n). Also how can you read a paragraph into a double vector of strings ( into words and strings again without using getLine)
When a data is read using getLine, it is generally read over the entire string input separated by space (' '). The input is terminated by a newline character ('\n').
For example - let us assume our input is - "A brown fox is hungry."
Then it can be stored in a array of string as {"A", "brown", "fox", "is", "hungry"}.
Separated by space and terminated by a newline character.
Code to illustrate
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NUMSTR 3 #define BUFFSIZE 100 int main() { char *words[NUMSTR]; char buffer[BUFFSIZE]; int i, count = 0, slen; /* loops only for three input strings */ for (i = 0; i < NUMSTR; i++) { printf("Enter a word: "); scanf("%s", buffer); words[count] = (char *) malloc(strlen(buffer) + 1); strcpy(words[count], buffer); count++; } /* reading input is finished, now time to print and free the strings */ printf("\nYour strings:\n"); for (i = 0; i < NUMSTR; i++) { printf("words[%u] = %s\n", i, words[i]); } return 0; }