Solved

delete trailing spaces

Posted on 2000-03-06
6
257 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 100 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
How to Post data to a PHP webpage from an embedded C application using TCP/IP? 5 243
how to understand recursion 12 232
Detect CR LF to each line 12 174
Socket Programming (Unix) 8 152
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…
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…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

679 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