Results 1 to 4 of 4

Thread: Remove spaces from string

  1. #1
    Join Date
    Aug 2006
    Posts
    139

    Remove spaces from string

    I have written code to Remove spaces from a string but it doesn't work...
    Code:
    stringspace string;
     for (int i = 0; i <str. length; i + +) (
        if (str [i]! = "")
         stringspace  + = str [i];
     )

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Remove spaces from string

    Try the following code to Remove All Spaces from a String :
    Code:
    void delspace (char *Str)
    {
    int Ptr = 0;
    int Dst = 0;
     
    while (Str [Ptr])
    {
    if (Str [Ptr] != ' ')
    	Str [Dst++] = Str [Ptr];
    Ptr++;
    }
     
    Str [Ptr] = 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Remove spaces from string

    Copy all the characters of this string to another string variable. While doing so, don't add the space charactes (space, or tab). ispunct()- finds the punctuation character. isspace()-finds the whitespace character.Check whether the current character is a space.That is, Scan the string character by character. If yes, then continue. If no, then copy this to the new variable. At the end of scanning, you have the string without character in the new string variable.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Remove spaces from string

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    void main() {
      char cspace[80] = "Where there is will,there is a way.";
      char *q1 = cspace;
      char *q2 = cspace;
      q1 = cspace;
       while(*q1 != 0) {
        if(ispunct(*q1) || isspace(*q1)) {
          ++q1;
        }
        else
        *q2++ = *q1++; 
      }
      *q2 = 0; 
      printf("\nAfter removing the spaces,string is:%s\n", cspace);
      getch();
    }

Similar Threads

  1. Enclosed spaces in Brink
    By Anantram in forum Video Games
    Replies: 6
    Last Post: 25-05-2011, 10:48 AM
  2. How to remove hyphens from string in asp.net?
    By Madaleno in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 09:30 PM
  3. how to remove certain characters from a String in java
    By Baazigar in forum Software Development
    Replies: 3
    Last Post: 13-02-2010, 08:02 PM
  4. Java - remove letters from string
    By LetsC in forum Software Development
    Replies: 3
    Last Post: 08-10-2009, 05:55 PM
  5. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,831,171.57282 seconds with 17 queries