Search code examples
stringvalidationpunctuation

C programming ispunct() function for validation


Currently I'm using string to collect input and I want to not allow the input have any punctuation except DOT(.)

For example :

char str[10];
int i,valid=1;

    do{
      printf("Input: ");
      fgets(str,10,stdin);

      for(i=0;i<strlen(str);i++)
      {
        if(ispunct(str[i])) //i want it to accept DOT(.)
        {
          printf("punctuation is not allowed!\n");
          valid=0;
          break;
        }
        else
          valid=1;
      }
    }while(valid==0);

Thank you in advanced for all professionals here.


Solution

  • You probably want:

         if(ispunct(str[i]) && str[i]!='.')