?
Solved

Trim left/right

Posted on 2003-02-27
6
Medium Priority
?
1,074 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

777 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