| | |
String Question
![]() |
•
•
Join Date: Apr 2009
Posts: 14
Reputation:
Solved Threads: 0
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:
Can you help me with that? Thank you all...
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:
C Syntax (Toggle Plain Text)
#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? Thank you all...
>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
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
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
How I would do it:
- Create a temporary variable of the same length as the one were you want to delete the spaces from
- 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.
- 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."
Last edited by tux4life; May 25th, 2009 at 5:05 pm.
"You can't build a reputation on what you are going to do."
i know the ultimate way
it's non-standard and not very portable, but hey, it works on my machine.
c Syntax (Toggle Plain Text)
#include<jephthah.h> newStr_ptr = removeSpacesAndCondense(myString);
it's non-standard and not very portable, but hey, it works on my machine.
>Or do you know the 'ultimate' way?
Why not?
Why not?
c Syntax (Toggle Plain Text)
char* condense(char* s) { if (s) { char* p = s; char* q = s; /* (never write so:) */ do if (*p != ' ') *q++ = *p; while (*p++); } return s; }
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: How Do I change Visual Studio 2005 Startup
- Next Thread: Encrypting strings with libmcrypt
Views: 446 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays asterisks binarysearch c++ calculate char command copyimagefile cprogramme creafecopyofanytypeoffileinc data database dev-c++ dice directory dynamic error evaluation factorial fgetc file fork forloop function functions givemetehcode givemetehcodez grade graphics hacking homework inches incrementoperators input kernel km lazy library linked linkedlist linux list lists locate logical_drives malloc matrix microsoft motherboard mysql no-code no-effort number opendocumentformat opensource output owf parallel pattern pdf performance pointer pointers posix precedence problem probleminc program programming radix read recursion recursive recv research scanf scripting segmentationfault sequential sms_speak socketprograming spoonfeeding standard string strtok structures student syntax system systemcall turbo-c turboc unix user variable visual wab windows






