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 6184063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:28:19+00:00 2026-05-24T01:28:19+00:00

#include <stdio.h> #include <string.h> int main() { char *slogan = together{kaliya} [namak]; char *slow_gun

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

int main() {

char *slogan = "together{kaliya} [namak]";
char *slow_gun = strdup(slogan);

char *token = strsep(&slow_gun, "{");

printf ("\n slow_gun: %s\n token: %s\n", slow_gun, token);

return 0;
}

when I execute it:

$ cc -o try try_strsep.c
$ ./try

 slow_gun: kaliya} [namak]
 token: together  

But when, I change the char *slogan to:

char *slogan = "kalia} [namak]";

and execute the same program:

$ vi try_strsep.c 
$ cc -o try try_strsep.c
$ ./try

 slow_gun: (null)
 token: kalia} [namak]

My Question is, so when I use strsep() and input string does not have the pattern I am looking for, the return of strsep() is wrong. The only way I can validate whether strsep() could not find the pattern is to check if (slow_gun == NUll).

If I have char *slogan = "together{" then strsep would successfully return token but returns slow_gun to blank (not null)

$ cc -o try try_strsep.c
$ ./try

 slow_gun: 
 token: together

Is there a way I could avoid this IF check and rely on the function to return me the substr and if its not there, return NULL?

  • 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-24T01:28:20+00:00Added an answer on May 24, 2026 at 1:28 am

    No, there’s no way to avoid the check slow_gun == NULL. Here’s a description of strsep‘s behavior:

    char *strsep(char **stringp, const char *delim);
    

    DESCRIPTION
    If *stringp is NULL, the strsep() function returns NULL and does nothing else. Otherwise, this function finds the first token in the string *stringp, where tokens are delimited by symbols in the string delim. This token is terminated by overwriting the delimiter with a null byte ('\0') and *stringp is updated to point past the token. In case no delimiter was found, the token is taken to be the entire string *stringp, and *stringp is made NULL.

    RETURN VALUE
    The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.

    So, if no match is found strsep returns a pointer to the original string and sets the slow_gun input to NULL.

    If the delimiter is the last character in the string, that character is overwritten by ‘\0’ and slow_gun is set to the following character, which happens to be the ‘\0’ terminating the original string. This is why print statement prints an empty string.

    NOTE You’re using strdup incorrectly, the caller is responsible for calling free on the pointer returned by that function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
I just translated this program, #include <stdio.h> int dam[1000][1000]; int main (int argc, const
So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
Can anyone explain to me why this isn't working? #include <stdio.h> #include <stdlib.h> char
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
How can I figure out the size of a file, in bytes? #include <stdio.h>
Where is the setting in Visual Studio 2003 to set the include path for

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.