Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

delete trailing spaces

Posted on 2000-03-06
6
Medium Priority
?
261 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

972 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