Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete trailing spaces

Posted on 2000-03-06
6
Medium Priority
?
260 Views
Last Modified: 2010-04-02
hello,
i have a table structure with 25 fields (all chars) and need to print them in a text format (csv).  i need to check each and every field data for null ('\0') and also delete their trailing spaces.  i tried using the for statement: for(x=0;x<(strlen(fld));x++) format but my code got so long because i kept repeating this statement and not exactly sure how to pass variables.  i'm writing this in SCO V platform.  could somebody please tell me how to make this work the easiest and simplest way possible.  some field data could have 3 or more words in it (like city) and i have to capture every character in it.  i really appreciate any help. thanks.

zandyl
0
Comment
Question by:zandyl
[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
6 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 400 total points
ID: 2588514
void strip(char *x)
{
while (strlen[x]>0 && x[strlen(x)-1]==' ')
       x[strlen(x)-1]='\0';
return;
}
0
 
LVL 1

Expert Comment

by:prozak
ID: 2588524
What you would want to do would be to pass in a char string and count to the ending null terminator:

void trim_and_print(FILE *output, char *fld) {
  int i=0;
  int lastchar=0;
  static char buf[256]; // Just to save on stack space

  // Look for last non-space character
  while(fld[i]) {
    if(fld[i] != ' ') {
      lastchar = i + 1;
    }
    i++;
  }

  if(lastchar > 0) {
   memcpy(buf, lastchar, fld);
   buf[lastchar] = '\0';
  }
  else
   sprintf(buf, "null");

  fprintf(output, "%s,", buf);
}

I haven't compiled or tested this code but it should give you an idea of what you need to do.
0
 
LVL 11

Expert Comment

by:alexo
ID: 2588666
Better:

    #include <string.h>
    #include <ctype.h>

    void strip(char* str)
    {
        char* p = str + strlen(str);
        while (--p >= str && isspace(*p))
            ;
        p[1] = 0;
    }
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:ntdragon
ID: 2588704
what do you mean by trailing spaces???
and don't do for(x=0;x<(strlen(fld));x++)

or any other loop with the strlen func in it it's very stupid 'cause you execute this func every time you get in the for
you can execute it only one time
i mean do:
int len=strlen(fld)
for(x=0;x<len;x++)

it will be better
0
 
LVL 1

Expert Comment

by:prozak
ID: 2588710
By calling strlen before the loop you're still going to loop through the string twice. If you're going to have to loop through the string anyway why even call strlen, just look for the \0 when looping through the string. That's all strlen going to do anyway.
0
 

Author Comment

by:zandyl
ID: 2588714
thanks cookre....

zandyl..:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

705 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