How to parse and validate a number in C

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024
  • Check out our Discord server: / discord

Комментарии • 10

  • @bikrae
    @bikrae 4 года назад

    YOU ARE A GENIOUS!!! been trying to find out how to validate numbers when a user inputs a no number value, this is simple and works just fine!!!

  • @kauealmeida
    @kauealmeida 3 года назад

    Bro, thanks for your channel!!! Now I will sucessed on algorithms in college. Keep with your great work

  • @Phantom-pj1ls
    @Phantom-pj1ls 4 года назад +1

    Wow Ive just discovered your channel! You're an amazing teacher!!
    So What about if my str is something like "123Abetc"
    How can I separate the int part from the char part to evaluate them separately?
    Ive read strtol can accomplish this but I haven't figured out how yet!
    Thank you very much!

    • @CodeVault
      @CodeVault  4 года назад +3

      Yes, you can use strtol to separate the two. Here's a code snippet:
      char str[] = "123ABetc";
      char strPart[100];
      char* endPtr;
      long int numPart = strtol(str, &endPtr, 10); // numPart will be 123
      // Here endPtr will point to the first letter in the str string.
      printf("%s
      ", endPtr); // Would print just the string part from the original string
      strcpy(strPart, endPtr); // strPart will be "ABetc", this will save the remamining string to another location (in strPart) which you can freely modify
      At the end:
      numPart will have the value 123
      strPart will have the value "ABetc"
      And thanks for the support, it is much appreciated!

  • @rodolfokey7835
    @rodolfokey7835 3 года назад

    Greta video; great explanation. Much better than the man pages in UNIX. Cheers mate

  • @wick6296
    @wick6296 4 года назад

    Hey ! Thank you for the vids ! Can you show me how to make a header or a file , where I can have commonly used funtions stored , for example , to use them faster when I need them . So , just to include the header , in use whatever function I need .

    • @CodeVault
      @CodeVault  4 года назад

      Sure thing, I can make a video on that. Expect it next week.

  • @deset1010
    @deset1010 2 года назад

    Thank you for the video. What about real numbers how to validate 123.45 ?

    • @CodeVault
      @CodeVault  2 года назад

      Try using strtof or strtod: www.cplusplus.com/reference/cstdlib/strtod/
      Basically the same as strtol but for real numbers

  • @sherzodernazarov7514
    @sherzodernazarov7514 Месяц назад

    thanks