| | |
Removing chars from a string
Thread Solved |
Hey its me again i dunno this string stuff gonna drive me insane really been doing since yesterday exercises on string did half of them but i have this program which should remove chars that i specify and when i test with putchar it does remove them but The string itself didnt !!!
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int RemoveChars(char *S,char c) { int i=0; int spaces=0; char temp; for(i=0;S[i]!=0;i++) { temp=S[i]; if(S[i]!=c) { S[i]=temp; putchar(temp);//this totally works !!!!!! } else spaces++; } return spaces; } int main(void) { char name[]="dude and duder"; int spaces; spaces=RemoveChars(name,' '); printf("Now after its Changed its %s and there was %d spaces\n",name,spaces);//What the Hell why doesnt it WORK !!!!!!! return 0; }
You should try sumthing like this:
1)return the spaces by the function(you r doing it..good)
2)Pass the char ** to the function and take an temporary sting remove spaces from that then assign address of the temp sting to your char** which is passed to the function.
1)return the spaces by the function(you r doing it..good)
2)Pass the char ** to the function and take an temporary sting remove spaces from that then assign address of the temp sting to your char** which is passed to the function.
C Syntax (Toggle Plain Text)
"dont print valuse using putchar inside the function."
Last edited by Dream2code; Aug 4th, 2009 at 7:21 am.
Here is your code and its logical error.
So basically, the reason why
So, I made some correction to your code and it works perfectly, I guess.
int RemoveChars(char *S,char c) {
int i=0;
int spaces=0;
char temp;
for(i=0;S[i]!=0;i++) {
temp=S[i];
if(S[i]!=c) {
// temp = S[i] and S[i] = temp, Hence, S[i] = S[i]
S[i]=temp;
putchar(temp);
}
else
spaces++;
}
return spaces;
}So basically, the reason why
putchar(temp); works is because you avoid printing character that you want to remove into the screen. However, your string that store in the memory remain the same.So, I made some correction to your code and it works perfectly, I guess.
C Syntax (Toggle Plain Text)
int RemoveChars(char *S,char c) { int count=0; int spaces = 0; char* temp; // allocate enough memory to store new string after remove. for(int i=0; S[i] != 0; i++) { if (S[i]==c) spaces++; else count++; } temp = (char*)malloc(count+1); // write new string without character you want to remove for(int j=0, i=0; S[i] != '\0'; i++) { if (S[i]!=c) temp[j++] = S[i]; } temp[count] = '\0'; // end string strcpy(S, temp); // copy new string to old string free(temp); // de-allocate new string memory. return spaces; }
Last edited by invisal; Aug 4th, 2009 at 10:00 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
![]() |
Similar Threads
- removing spaces from string (C++)
- Replace Chars in String Problem (JavaScript / DHTML / AJAX)
- Removing punctuation from a string (C++)
- Removing characters from a string (C)
- Removing found chars from string (C)
Other Threads in the C Forum
- Previous Thread: Need help for run time speaker indicator
- Next Thread: dice probability
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h





