How to take this line as input in c ,deliminated by "--*

Dhruvil Doshi–11/02/2019–Google Corp

output should be

Dhruvil Doshi 11/02/2019 Google Corp

hello @dhruvilcodes
first read it as string
and then use strtok function with delimeter - to parse it

1 Like

it is “–” I am not getting it I can parse by - but can’t by double dash

yeah that may cause some issue becuase compiler treats each character of delimeter string (the string we pass in strtok) as delimeter.

pls share ur code that u r running

Hello sir ACTUALLY I RUN THIS AND IT WORKED;

while(token!=NULL)

    {

        strcpy(part[i],token);

        token=strtok(NULL,"-");

        i++;

    }

u r passing single dash here

its working even for double dash

#include <string.h>
#include <stdio.h>

int main () {
   char str[80] = "Dhruvil Doshi--11/02/2019--Google Corp";
   const char s[3] = "--";
   char *token;
   
   token = strtok(str, s);
   

   while( token != NULL ) {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}
1 Like

cleared my doubt but sir now I have doubt why my single dash worked?

double dash is being treated as two single dash so ur string is just equvalent to some english characters along with pair of single characters.

#include <string.h>
#include <stdio.h>

int main () {
   char str[80] = "Dhruvil Doshiasdf11/02/2019asdfGoogle Corp";
   const char s[5] = "asdf";
   char *token;
   
   /* get the first token */
   token = strtok(str, s);
   
   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}

run this and check what output u r getting.

Dhruvil Do
hi
11/02/2019
Google Corp

i am getting this output.

yes becuase in delimiter we have given asdf , it will remove all charcters thats are part of this string

1 Like

GOT IT SINGLE - OR DOUBLE - WONT’ MATTER.

yes … … … …

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.