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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:58:03+00:00 2026-06-12T06:58:03+00:00

I need to split a string into string based on the location of a

  • 0

I need to split a string into string based on the location of a character. So that:

str1 = “hello?world” is str1 = “hello” and str2 = “world”

This is what I have so far:

    char    str1[100] = "hello?world";
    char    str2[100];
    char    *p;

    p = strpbrk(str1, "?");
    strcpy(&str2, p);

    strcspn(str1, '?');

I get errors when trying to copy the characters after ‘p’ to str2. There has got to be a better and functional way. Can someone help me out? Many thanks…

  • 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-06-12T06:58:05+00:00Added an answer on June 12, 2026 at 6:58 am

    This

    strcspn(str1, '?');
    

    is an error, strcspn‘s second parameter is a const char*, passing a character constant there is almost certain to cause undefined behaviour and a segmentation fault (Your programme is very unlikely to have the address 63 (ASCII value of ‘?’) in its address space, and if it has, it’s unlikely to point to a 0-terminated character array).

    p = strpbrk(str1, "?");
    strcpy(str2, p+1);
    

    ought to work. If you want to end str1 at the ‘?’, *p = 0; overwrites the ‘?’ with a 0. But of course generally, you should verify that strpbrk doesn’t return NULL before using p.

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

Sidebar

Related Questions

I have this string MIL_A_OP=LI_AND=SSB12=JL45==DO=90==IT=KR002112 and I need to split it into 2 ,
I need to split comma delimited string into a second columns I have the
I have a problem where I need to split an input string into possible
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I have a long string (multiple paragraphs) which I need to split into a
I need to take a string and split it into an array based on
I have the following string that I need to split from a field called
I need to split a string into chunks of 2,2,3,3 characters and was able
I need to split a string into two parts. The string contains words separated
I need to split my string input into an array at the commas. Is

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.