Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,557 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,914 people online right now. Registration is fast and FREE... Join Now!




remove space in string

 
Reply to this topicStart new topic

remove space in string

vboy
17 May, 2008 - 10:57 PM
Post #1

New D.I.C Head
*

Joined: 17 May, 2008
Posts: 1

i got problem with string
how to remove space from user input
example

input : help(space)(space)(space)(space)(space)(space)my(space)(space)problem

output:
help my problem

just one space accepted smile.gif

this my source code that i can make,but it's totaly wrong,i guest... tongue.gif
CODE

#include<stdio.h>
#include<string.h>

void main()
{
    char kal[100];
    int n,index;
    printf("input sentences :");
    gets(kal);
    puts(kal);
    
    index=0;
    n=strlen(kal);
    for(int i=0;i<n;i++)
    {
        if((kal[i]==' ') || (i==n-1))
        {
            if (i==n-1)
            {
                i=i+1;
            }
            for(int j=i;j<index;j++)
            {
                printf("%c",kal[j]);
            }
            index=i;
            printf(" ");
        }
    }

}


plz help me..... thx.......

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Remove Space In String
17 May, 2008 - 11:02 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
** Moved topic to C/C++ **
User is offlineProfile CardPM
+Quote Post

Wr4i7h
RE: Remove Space In String
18 May, 2008 - 01:08 AM
Post #3

New D.I.C Head
*

Joined: 16 May, 2008
Posts: 14


My Contributions
This is how i would do it. Hope this helps smile.gif

CODE
#include<iostream>
#include<stdio.h>
#include<string.h>

using namespace std;

int main(){
    
    char kal[100];
    int n,index;
    printf("input sentences :");
    gets(kal);
    
    int i(0),j(0);
    char newkal[100];
    
    for (i=0;i<100;i++){
        if (kal[i] != ' '){
           newkal[j] = kal[i];
           j++;            
        }
    }
    
    puts(newkal);
    
system("pause");
return 0;
}


User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Remove Space In String
18 May, 2008 - 05:34 AM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
Wr4i7h missed the
QUOTE
just one space accepted
part, and included a C++ header (iostream) for some reason... Here is how I would do it.

CODE
#include <stdio.h> /* printf(), gets() */

int main(void)
{
  char str[100];
  int  read  = 0; /* Index of the charcter we will be reading from the array  */
  int  write = 0; /* Index we will be writing the character to in the array   */

  printf("input sentences :");
  gets(str);

    /* Loop until we find a null terminator (0)                               */
  while(str[read])
  {
      /* Copy over each character until we get to a space. We increment read  *
       *   and write here, since we want to copy everything up to a space     */
    while(str[read] && str[read] != ' ')
      str[write++] = str[read++];

      /* We broke out if we found the null terminator, or a space. If a space *
       *   was found, we want to keep the first space.                        */
    if(str[read])
      str[write++] = str[read];

      /* Loop until we find something other then a space. We just increment   *
       *   read here, since we want to ignore the additional space characters.*/
    while(str[read] && str[read] == ' ')
      ++read;
  }

    /* Add the new null terminator                                            */
  str[write] = 0;

  printf("%s\n", str);

  return 0;
}


edit: Didn't need string.h

This post has been edited by Cerolobo: 18 May, 2008 - 05:36 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month