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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:22:06+00:00 2026-06-09T14:22:06+00:00

I am using this question as a guide to trimming a string in C.

  • 0

I am using this question as a guide to trimming a string in C. It works properly on a string bounded exclusively by whitespaces (' '), but on special whitespaces ('\r', '\n', '\t', etc.), it fails. Here’s an example:

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

size_t trim(char *out, size_t len, const char *str)
{
  if(len == 0)
    return 0;

  const char *end;
  size_t out_size;

  // Trim leading space
  while(isspace(*str)) str++;

  if(*str == 0)  // All spaces?
  {
    *out = 0;
    return 1;
  }

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;
  end++;

  // Set output size to minimum of trimmed string length and buffer size minus 1
  out_size = (end - str) < len-1 ? (end - str) : len-1;

  // Copy trimmed string and add null terminator
  memcpy(out, str, out_size);
  out[out_size] = 0;

  return out_size;
}

int main(){

    char *str = " \n\n  hello  \t    \r  ";
    char trimmed[strlen(str)];

    trim (trimmed, strlen(trimmed), str);
    printf("~%s~\n~%s~\n", str, trimmed);

    return 0;
}

produces the output:

~ 

  ~ello         
~~

can anyone fix the code to trim all whitespace characters properly?

Second Question: The first function in the referenced answer gives me a segfault. does anyone know why this is the case?

  • 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-09T14:22:08+00:00Added an answer on June 9, 2026 at 2:22 pm

    The way you called the function is incorrect.

    char *str = " \n\n  hello  \t    \r  ";
    char trimmed[strlen(str)+1]; // Note that you must +1 for the terminating \0.
    
    // Use sizeof() instead of strlen() because trimmed is containing garbage.
    // strlen() measures the length of the content while sizeof() measure the allocated size of the array.
    trim (trimmed, sizeof(trimmed), str); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this previous question as a guide, I've attempted to create a ul navigation
I am trying to integrated linkedIn using this question answer Posting LinkedIn message from
Unlike this question: Linker Error while building application using Boost Asio in Visual Studio
Similar to this question I am using a custom VirtualPathProvider to retrieve views that
Note: In this question I'm using the term autocomplete (or iterative search) to refer
Im using the answer to this question to convert an array of XML to
This question is related to another question I wrote: Trouble using DOTNET from PHP.
This question is related to this SO post Rather than using a recursive CTE
This question came about because the cells gem specifies template directories using File.join('app','cells'). That
Based on this question How to insert array into mysql using PDO and bindParam?

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.