Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 793183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:06:36+00:00 2026-05-14T22:06:36+00:00

copy string between comma input (aaa),(ddD),(sss),(ppp) p=malloc(sizeof(char)*200); gets(p); i want hold input p[0]=(aaa) p[1]=(ddD)

  • 0

copy string between comma

input

(aaa),(ddD),(sss),(ppp)

p=malloc(sizeof(char)*200);
gets(p);

i want hold input

p[0]="(aaa)"
p[1]="(ddD)"
p[2]="(sss)"
p[3]="(ppp)"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:06:37+00:00Added an answer on May 14, 2026 at 10:06 pm

    You may have to use strtok.

    Here is the complete solution to all your problems:

    // tokens.c
    
    #include <stdio.h>
    #include <string.h> /* for strtok, strlen and strcpy. */
    #include <stdlib.h> /* for malloc, realloc and free. */ 
    
    static char **tokens = NULL; /* Dynamic array of string tokens. */
    static int token_count = 0; /* Number of tokens added. */
    
    /* Grows the `tokens' array as needed and appends `tok' to it. */
    static void
    copy_token (char *tok)
    {
      if (token_count == 0)
        tokens = malloc (sizeof (char*));
      else
        tokens = realloc (tokens, sizeof (char*) * (token_count + 1));
      tokens[token_count] = malloc (strlen (tok) + 1);
      strcpy (tokens[token_count], tok);
      ++token_count;
    }
    
    /* Extracts tokens from `s' and calls copy_token to add it to `tokens'. */
    static void
    tokenize_by_comma (char *s)
    {
      char *tok = strtok (s, ",");
      while (tok != NULL)
        {      
          copy_token (tok);
          tok = strtok (NULL, ",");
        }  
    }
    
    /* If you run copy_after, the total length of all tokens 
       must not exceed BUFF_SIZE. */
    #define BUFF_SIZE 1024 
    static char s_copy[BUFF_SIZE + 1];
    
    /* Makes a string of all the tokens by moving `s' next to `after'. */
    static char *
    copy_after (const char *s, const char *after)
    {
      int i;
      int appended = 0;
      strcpy (s_copy, "");
      for (i = 0; i < token_count; ++i)
        {
          int is_s = (strcmp (tokens[i], s) == 0);
          int is_after = (strcmp (tokens[i], after) == 0);
          if (is_after)
            {
              strcat (s_copy, after);
              strcat (s_copy, ",");
              strcat (s_copy, s);
              appended = 1;
            }
          else if (!is_s)
            {
              strcat (s_copy, tokens[i]);
              appended = 1;
            }
          if (i != (token_count - 1) && appended) 
            strcat (s_copy, ",");
          appended = 0;
        }
      return s_copy;
    }
    
    /* Prints the `tokens'. */
    static void 
    print_tokens ()
    {
      int i;
      for (i = 0; i < token_count; ++i)
        printf ("%s\n", tokens[i]);
    }
    
    /* Frees the memory allocated for `tokens'. */
    static void 
    free_tokens ()
    {
      int i;
      for (i = 0; i < token_count; ++i)
        free (tokens[i]);
      free (tokens);
      token_count = 0;
      tokens = NULL;
    }
    
    /* Test. Pass the tokens as a single command line argument. */
    int
    main (int argc, char **argv)
    {
      tokenize_by_comma (argv[1]);
      print_tokens ();
      if (argc == 4)
        {
          printf ("%s\n", copy_after (argv[2], argv[3]));
        }
      free_tokens ();
      return 0;
    }
    

    Test run:

    $ ./tokens "(aaa),(ddD),(sss),(ppp)"
    (aaa)
    (ddD)
    (sss)
    (ppp)
    $ ./tokens "(aaa),(ddD),(sss),(ppp)" "(ddD)" "(ppp)"
    (aaa)
    (ddD)
    (sss)
    (ppp)
    (aaa),(sss),(ppp),(ddD)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 397k
  • Answers 397k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From the documentation: If you specify nil for the nibName… May 15, 2026 at 3:11 am
  • Editorial Team
    Editorial Team added an answer let thousands(x:int64) = String.Format("{0:#,0}", x) May 15, 2026 at 3:11 am
  • Editorial Team
    Editorial Team added an answer You could store the value into the Session and retrieve… May 15, 2026 at 3:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.