Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2005, 07:57 AM   #1 (permalink)
xytor
Registered User
 
Join Date: Sep 2005
Posts: 4
xytor is on a distinguished road
removing leading and trailing whitespace from a string

Hi.
I want to remove all leading and trailing whitespace from a string, but i don't know how.
xytor is offline   Reply With Quote
Old 09-14-2005, 10:08 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Is this in C ?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
  char *str_trim, str[] = "   the string with leading and trailing spaces    ";
  int i, j, stop=0, length=strlen(str);
  /* create space for the copying */
  str_trim = (char*) malloc(length*sizeof(char));
  if(!str_trim)
    {
      printf("Error not enough heap space\n");
      return -1;
    }
  /* remove all leading whitespaces */
  for(i=0, j=0; str[i]; ++i)
    if(!stop && (str[i] == ' ' || str[i] == '\t'))
      ; /* skip it */
    else
      {/* copy everything after the leading spaces */
	stop=1;
	str_trim[j++] = str[i];
      }
  str_trim[j] = '\0';
  /* remove any tailing whitespace */
  length=strlen(str_trim) -1;
  for(;str_trim[length] == ' ' || str_trim[length] == '\t'; length--)
    ; /* just skoot on down there */
  /* eliminate any further whitespaces */
  str_trim[length +1] = '\0';
  printf("Results:\n");
  printf("\tOrrig string: \"%s\"\n", str);
  printf("\tTrim string: \"%s\"\n", str_trim);
  free(str_trim);
  return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 09-14-2005, 10:37 AM   #3 (permalink)
xytor
Registered User
 
Join Date: Sep 2005
Posts: 4
xytor is on a distinguished road
well, it works stand-alone but i need to integrate it into my program, which i am having trouble doing.

Pretty much, all i need to figure out is how to convert a string to an array of chars, and then an array of chars to a string, and i'm good
xytor is offline   Reply With Quote
Old 09-14-2005, 11:11 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
look at c_str like:
Code:
std::string foo("bar");
char str[] = foo.c_str();
And for the reverse it's just to look at the constructors to see:
Code:
std::string ret_str(str);
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 01:29 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting