Solved

delete trailing spaces

Posted on 2000-03-06
6
248 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now