waltermilner

Untitled

Apr 30th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. int main ()
  7. {
  8.     long n;
  9.     char temp[17]; // input buffer
  10.     do
  11.     {
  12.         printf("How many  strings?: ");
  13.         fgets(temp, 16, stdin); // input
  14.         n=strtol(temp,NULL,10); // convert to number
  15.     }
  16.     while (n==0); // failure gives 0 if they do not enter a valid integer
  17.     char name[n][17];
  18.     int i =0;
  19.  
  20.     for(i=0; i<n; i++)
  21.     {
  22.         printf("Input string number %d: ", i);
  23.         do
  24.         {
  25.             fgets(temp, 16, stdin); // get input, max 16 chars
  26.             char * where = strchr(temp,'\n'); // look for Enter
  27.             if (where) // if got one
  28.                 *where=0; // replace by 0
  29.             printf("Length of temp = %d temp = %s\n",strlen(temp), temp); // check what we've got
  30.         }
  31.         while (strlen(temp)<1); // in case they just press Enter
  32.         strcpy(name[i], temp);
  33.     }
  34.     // check what we have:
  35.     for(i=0; i<n; i++)
  36.     {
  37.         printf("%s\n",name[i]);
  38.     }
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment