Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Trim left/right

Posted on 2003-02-27
6
Medium Priority
?
1,083 Views
Last Modified: 2012-06-27
Hi ,
just wanna know where to get the trim left and trim right
function in pure C.
in Visual C++ it is <string>.Left(int) and <string>.Right(int)
thanks.
0
Comment
Question by:chekban
  • 2
  • 2
  • 2
6 Comments
 
LVL 1

Expert Comment

by:Linzer
ID: 8032504
The following solution was previously proposed (and accepted) in EE:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20108978.html

----------------------------------------------------

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

void trim(char string[])
{
                         int runner;
                         size_t strLength = strlen(string);
                             
                         for (runner=0;runner<strLength;runner++) {
                              if (*(string+runner)!=' ' && *(string+runner)!='\t') break;
                         }

                         strcpy(string,string+runner);

                        strLength = strlen(string);

                         for (runner=1;runner<strLength;runner++) {
                              if (*(string+strLength-runner)!=' ' && *(string+strLength-runner)!='\t') break;
                         }

                         *(string+strLength-runner+1) = 0;

}

---------------------------------------------------

I find it quite satisfying.
0
 
LVL 10

Expert Comment

by:makerp
ID: 8032535
char *trim(char *str)
{
  int start;
  int end;
  char *retval;
  for(start=0;start<strlen(str);start++)
    if(str[start] != ' ' && str[start] != '\t')
      break;
  for(end=strlen(str) - 1;end > start;end--)
    if(str[end] != ' ' && str[end] != '\t')
      break;
  retval = (char*)malloc((end - start) + 1);
  strncpy(retval,str + start,end - start);
  return retval;
}
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 100 total points
ID: 8033340
To my knowlwdge there is no such functions available in common C. For that you have to write your own routines. I have designed two simple functions for this purpose.void

ltrim(char *str)
{
     int i = 0;
     int length = strlen(str);
     while(str[i]==' ')
          i++;
     strncpy(str, (str+i), length - i);
}

void rtrim(char *str)
{
     int length = strlen(str);
     int i = length - 1;
     while(str[i]==' '&& i!=0)
          i--;
     str[i+1] = '\0';
}

Just check it out. using it is very simple.
Hope this may help you.
Regards
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:Linzer
ID: 8033424
Hey guys, what do your functions add compared to my first post?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 8033522
Reply to Linzer:

You have not given any solution for ltrim and rtrim seperately. What if user wants to use the ltrim only not the rtim. Try to modularize the problem.
0
 
LVL 10

Expert Comment

by:makerp
ID: 8043981
>>Linzer

who cares......

these sorts of questions always get every body splashing a bit of code up, its up to the asker to decide what one they like the best
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question