Advertisement
Yesideez

Reading textual input

Jan 1st, 2019
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void trimCR(char *t) {
  6.   if (t[strlen(t)]=='\0') {t[strlen(t)-1]='\0';} //set last byte-1 to null
  7. }
  8.  
  9. void main() {
  10.   char tmp[64];
  11.   int num;
  12.   printf("Enter something: ");
  13.   if (fgets(tmp,sizeof tmp,stdin)) {
  14.     num=atoi(tmp); //extract a number from our text
  15.     trimCR(tmp);
  16.     printf("I found the number %d inside '%s' which is %ld characters!\n",num,tmp,strlen(tmp));
  17.   } else {
  18.     printf("There seems to be an error with your input\n");
  19.   }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement