LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2004, 11:04 PM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
striping white space and line


I need to write a c program which would strip off all white space, tab, line feed of the beginning and end of a string.

Can anyone suggest a good way?
 
Old 11-21-2004, 11:51 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I'd suggest 2 loops. One that iterates through from the beginning of the string and one from the end. In the first loop, you can use memmove() to keep copying index+1 to index as long as string[index] is a space, tab, or new line. In the second loop you can just keep setting string[index] to '\0' as long as string[index] is a space, tab, or new line.
 
Old 11-22-2004, 02:04 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
char *trim(char *s)
{
  int n=strlen(s)-1;
  while(*s && *s<=' ')
  {
    ++s;
  }
  s=strdup(s);
  while(n && s[n] <= ' ')
  {
    s[n--]='\0';
  }
  return(s);
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
White line around panel hokputooy Linux - Software 3 11-30-2005 02:37 AM
white space in eterm scrollback buffer asouldja Linux - Software 0 05-03-2005 06:49 AM
KDE 3.3 and X.Org - Strange millimetre of white space under all windows StephenCM Linux - Software 0 01-14-2005 02:29 PM
removing white space accent11 Linux - Software 4 10-06-2004 01:30 AM
Two Tuxes with block of white space??? robbow52 Debian 1 07-04-2004 01:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration