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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:20:46+00:00 2026-06-15T03:20:46+00:00

I have a loop running to add spaces between some special characters (&,|,<,>). The

  • 0

I have a loop running to add spaces between some special characters (&,|,<,>). The following code successfully adds spaces before and after a special character:

    char keys[] = "<>&|";
    int i = strcspn (input,keys); 
    appenda(input, " " , i);
    appenda(input, " " , i+2);

The above code converts input “asdf&asdf” to “asdf & asdf”.

However, my goal is to do that for every special character in the entire input even with multiple special characters (like “asdf&asdf&asdf”). So I made a while loop:

    char keys[] = "<>&|";
    int i = strcspn (input,keys);
    while(i < strlen(input)){
        appenda(input, " " , i);
        appenda(input, " " , i+2);
        i = strcspn (input,keys);
    }

Yet, when I run my code now, it returns with “*** stack smashing detected ***“

Any Ideas on what this means and how to get around it?

EDIT
Appenda inserts a string into another string at a designated point. it takes in 3 arguments: 1st is a string which i insert into, 2nd is a string which i want to insert and 3rd is the index. so appenda(ABCD, X, 2) returns AXBCD

  • 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-15T03:20:47+00:00Added an answer on June 15, 2026 at 3:20 am

    You’re always calling strcspn on the same input, and you’re never actually removing the characters that it’s finding, so it is always finding the same one.

    For example, if your string is

    asdf&asdf&asdf
    

    The first call to

    int i = strcspn (input,keys)
    

    Returns i = 3 since the & is in position 4. Then you insert the spaces, which works fine, and the string becomes:

    asdf & asdf&asdf
    

    Now you again call

    i = strcspn (input,keys)
    

    And this returns i = 4 because now it finds the first & in position 5. So when you insert spaces again the string becomes:

    asdf  &  asdf&asdf
    

    And so on. It keeps inserting more and more spaces around the first &, and the loop never ends until you overrun the buffer inside appenda and your program dies.

    Instead, once you’ve inserted spaces, you need to tell strcspn to start looking for the next special character past the place where you found the previous one. This should work:

    char keys[] = "<>&|";
    int i = strcspn (input,keys);
    while(i < strlen(input)){
        appenda(input, " " , i);
        i += 2;
        appenda(input, " " , i);
        i += strcspn (input + i,keys);
    }
    

    This “moves” the value of i such that when you call strcspn(input + i, keys), the value input + i always refers to the next position that has not yet been looked at for special characters.

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

Sidebar

Related Questions

Suppose you have the following function foo . When I'm running a for loop,
I have a Parallel.ForEach loop running an intensive operation inside the body. The operation
I have a program in Octave that has a loop - running a function
I have a PHP script with infinite loop. I need this script running forever.
I have a perfectly fine running FragmentActivity with a smooth horizontal sliding between the
I have a class with a method that is running a nested ParallelFor loop.
I'm running into this strange issue. Basically I have a loop that loops forever,
I have successfully built a login form using ajax and want to add a
I am using some dell workstations(running WinXP Pro SP 2 & DeepFreeze) for development,
I have the below code which goes through and returns disk information. When running

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.