String Question

Please support our C advertiser: Download Intel® Parallel Studio Eval
Reply

Join Date: Apr 2009
Posts: 14
Reputation: newbiecoder is an unknown quantity at this point 
Solved Threads: 0
newbiecoder newbiecoder is offline Offline
Newbie Poster

String Question

 
0
  #1
May 25th, 2009
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 : d a n i w e b
I want to make it : daniweb

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

  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5. int i;
  6. char a[21];
  7.  
  8.  
  9.  
  10. gets(a);
  11.  
  12. for(i = 0; i < 20; i++)
  13. {
  14. if(a[i] == 8 || a[i] == 9)
  15. {a[i]= a[i+1];
  16. i--;}
  17. else continue;
  18. }
  19.  
  20. printf("%s", a);
  21. printf("\n");
  22.  
  23. return 0;
  24. }

Can you help me with that? Thank you all...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: String Question

 
0
  #2
May 25th, 2009
>I wrote this code, but it didn't change anything
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 did you want to achieve with so strange a[i] == 8 || a[i] == 9 condition?

It's a very simple assignment. Please, think again then caome back with a proper code tag:
[code=c]
source
[/code]

Or search DaniWeb more carefully: there are lots of right solutions of this assignment
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,008
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 218
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: String Question

 
0
  #3
May 25th, 2009
How I would do it:
  1. Create a temporary variable of the same length as the one were you want to delete the spaces from
  2. Use a for-loop to loop through the whole string where you want to delete the spaces from, every time you check whether the read character is a space or another character, when the character is a space, don't copy it to the temporary string, otherwise: copy it to the temporary string.
  3. Eventually you could use strcpy to copy the string without the spaces to the variable which was holding the string with the spaces (note that it will be overwritten then!)
Last edited by tux4life; May 25th, 2009 at 4:48 pm.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: String Question

 
1
  #4
May 25th, 2009
Remember KISS principle: no need in temporary strings!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,008
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 218
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: String Question

 
0
  #5
May 25th, 2009
Originally Posted by ArkM View Post
Remember KISS principle: no need in temporary strings!
I thought that was the simplest way, however he could also just shift each character to the left if there's a space in between. Or do you know the 'ultimate' way?
Last edited by tux4life; May 25th, 2009 at 5:05 pm.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,673
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: String Question

 
2
  #6
May 26th, 2009
Originally Posted by tux4life View Post
Or do you know the 'ultimate' way?
i know the ultimate way

  1. #include<jephthah.h>
  2.  
  3. newStr_ptr = removeSpacesAndCondense(myString);

it's non-standard and not very portable, but hey, it works on my machine.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: String Question

 
1
  #7
May 26th, 2009
>Or do you know the 'ultimate' way?
Why not?
  1. char* condense(char* s)
  2. {
  3. if (s) {
  4. char* p = s;
  5. char* q = s;
  6. /* (never write so:) */
  7. do if (*p != ' ')
  8. *q++ = *p;
  9. while (*p++);
  10. }
  11. return s;
  12. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 446 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC