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 !!!

#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;
}

Its working fine with me..!!!No problem at all!!!

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.

"dont print valuse using putchar inside the function."

Here is your code and its logical error.

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.

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;
}
commented: good little answer +2

thanks i was using putchar as to debug output but if i did temp as char array would it work 2 ?

thanks i was using putchar as to debug output but if i did temp as char array would it work 2 ?

You can also use temp as char array, but you don't know how much element you need for your new string...

i think it can be with VLA variables

anyways thanks case solved :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.