941,512 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 579
  • C RSS
May 25th, 2009
0

String Question

Expand Post »
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
newbiecoder is offline Offline
60 posts
since Apr 2009
May 25th, 2009
0

Re: String Question

>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
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 25th, 2009
0

Re: String Question

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 25th, 2009
1

Re: String Question

Remember KISS principle: no need in temporary strings!
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
May 25th, 2009
0

Re: String Question

Click to Expand / Collapse  Quote originally posted by ArkM ...
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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 26th, 2009
2

Re: String Question

Click to Expand / Collapse  Quote originally posted by tux4life ...
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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
May 26th, 2009
1

Re: String Question

>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. }
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How Do I change Visual Studio 2005 Startup
Next Thread in C Forum Timeline: Encrypting strings with libmcrypt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC