C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-25-2009, 12:05 PM   #1
Registered User
 
Join Date: Apr 2009
Posts: 10
Unhappy Deleting spaces in a string

Hi,
I want to write a code which takes an array of 20 characters and deletes all spaces between characters, it should leave only letters. For example:
input :a b c d e
I want to make it : abcde

I wrote this code, but it didn't change anything:


Code:
#include <stdio.h>

 int main ()
 {
         int i;
         char a[21];



         gets(a);

         for(i = 0; i < 20; i++)
         {
                 if(a[i] == 8 || a[i] == 9)
                 {a[i]= a[i+1];
                 i--;}
                 else continue;
         }

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

         return 0;
 }

Can you help me with that? Thanks...
bahada is offline   Reply With Quote
Old 05-25-2009, 12:11 PM   #2
Registered User
 
Join Date: Apr 2009
Posts: 187
your searching if a[i]==8 or 9 whish is back space or horiztal tab then you making i+1 to that a and minunsing i after that ii dunno why your doing that anyways ?
why dont you try copy anything inside the string thats not space
if(a[i]!=' '){continue your function here}?
elwad is offline   Reply With Quote
Old 05-25-2009, 12:34 PM   #3
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Code:
if(a[i] == 8 || a[i] == 9) /* what is this statement supposed to do??? */
itCbitC is offline   Reply With Quote
Old 05-25-2009, 12:36 PM   #4
Registered User
 
Join Date: Apr 2009
Posts: 10
to catch if the character is a space or not (backspace and horizontal tab)
bahada is offline   Reply With Quote
Old 05-25-2009, 12:39 PM   #5
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
' ' == 32

The other complication is that "i--" is not sufficient to compress the string.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 05-25-2009, 12:50 PM   #6
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Then use the character constant instead of its decimal value which depends on the underlying character set.
itCbitC is offline   Reply With Quote
Old 05-25-2009, 12:55 PM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
Don't do this
String Question - C

Here's why
How To Ask Questions The Smart Way
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-25-2009, 01:17 PM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by Salem View Post
Don't do this
Salem is being mean! There is nothing wrong with the OP. The one and only reply on Daniweb starts out with a completely moronic and incorrect analysis:
Quote:
You can't compile this code, that's why it didn't change anything: the else alternative has no correspondent if! Didn't you see it?
What? I see an if and an else. I'd go somewhere "else" and ask again too.

Chill out bahada.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 05-25-2009, 11:46 PM   #9
Registered User
 
Join Date: Apr 2009
Posts: 10
I solved this problem in another way thanks, and I asked at first at daniweb but i couldn't get the answer for hours, then I asked here..
bahada is offline   Reply With Quote
Old 05-26-2009, 07:08 PM   #10
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
function for deleting any character
Attached Files
File Type: c delete_char_line.c (918 Bytes, 33 views)
c.user is offline   Reply With Quote
Old 05-26-2009, 10:00 PM   #11
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> but i couldn't get the answer for hours, then I asked here
Oh, but there are answers there.
So now you're claiming it's urgent
Don't go round spamming multiple forums in the mistaken belief that you're entitled to a 24/7 service wherever you go.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-26-2009, 10:07 PM   #12
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by Salem View Post
Don't go round spamming multiple forums in the mistaken belief that you're entitled to a 24/7 service wherever you go.
That is not spamming, and you are not obliged to answer questions...I think.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Tags
array, character, delete, space, string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
Inheritance Hierarchy for a Package class twickre C++ Programming 7 12-08-2007 04:13 PM
Custom String class gives problem with another prog. I BLcK I C++ Programming 1 12-18-2006 03:40 AM
Compile Error that i dont understand bobthebullet990 C++ Programming 5 05-05-2006 09:19 AM
problems in removing white spaces from string of text monil C Programming 1 03-08-2005 12:02 PM


All times are GMT -6. The time now is 09:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22