• C

Trim left/right

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.
chekbanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LinzerCommented:
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
makerpCommented:
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
rajeev_devinCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

LinzerCommented:
Hey guys, what do your functions add compared to my first post?
0
rajeev_devinCommented:
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
makerpCommented:
>>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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.