Solved

delete trailing spaces

Posted on 2000-03-06
6
250 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

896 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

11 Experts available now in Live!

Get 1:1 Help Now