Azuan
Azuan

Reputation: 928

how to find non-alphanumeric in C

im trying to separate non-alphanumeric and alphanumeric from a string in C.this is my current code,but if i use this,it will detect all alphanumeric only,while at non-alphanumeric it will return null.so i cant detect at what index are all the non-alphanumeric at.

    char data[] = "http://www.google.com";
    char key[]  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    char *find;

    find = strpbrk(data,key);
    while(find != NULL){
        printf("%c",*find);
        find = strpbrk(find+1,key);        
    }

The output will be httpwwwgooglecom.Thats is what i partially want. i also trying to find where are all the non-alphanumeric at.

Upvotes: 0

Views: 5024

Answers (2)

Seth Robertson
Seth Robertson

Reputation: 31451

Sometimes, it is just easier to do the work yourself. And yes, for the specific case of A-Za-z0-9, you should use isalnum() instead of strchr().

for(c=data;*c;c++)
{
  if (!strchr(key,*c))
   {
     // Do something with non-alpha
   }
   else
   {
      printf("%c",*c);
   }
}

Upvotes: 4

ribram
ribram

Reputation: 2450

Have a look at the C isalpha family of routines.

Upvotes: 4

Related Questions