C char[] single character removal *SOLVED*

Programming, for all ages and all languages.
Post Reply
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

C char[] single character removal *SOLVED*

Post by Troy Martin »

Okay, I have a char array that I have the location of two characters that I need to remove. I tried setting them to '\0' but that didn't work. What can I do? The locations in the string are strlen(input)-6 and strlen(input)-5.

Background information: I'm implementing a line-by-line C cross-compiler for an OS (I don't think I should mention the name) and I'm using this to implement a final "\n" in puts().
Last edited by Troy Martin on Thu Aug 28, 2008 5:29 pm, edited 1 time in total.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: C char[] single character removal

Post by Alboin »

Why not just copy the string over itself, thus overwriting the unwanted parts?

Note: '\0' indicates the end of the string. Overwriting a character with it would just prematurely end the string.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: C char[] single character removal

Post by AndrewAPrice »

Out of curiosity, why are you modifying the string?
I read the source in from a stream byte-by-byte using an iterator, and if I wanted to skip over a character I just increased the iterator.
My OS is Perception.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: C char[] single character removal

Post by DeletedAccount »

Man , use a debugger and find out what exactly is going on ... , set breakpoints etc ...

Regards
Sandeep
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: C char[] single character removal

Post by AJ »

SandeepMathew wrote:Man , use a debugger and find out what exactly is going on ... , set breakpoints etc ...
Agree with the debugger bit (a memory dump may have helped the OP although given the original question that may be assuming too much), but out of interest where would it help to put a breakpoint to help with this query? The OP already knows that things go wrong after replacing the characters with '\0'.

Cheers,
Adam
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: C char[] single character removal

Post by DeletedAccount »

Hi ,
I really not know where exactly :oops: . But i might set it where the function gets called . Also i do not have any details here .
Regards
Sandeep
User avatar
B.E
Member
Member
Posts: 275
Joined: Sat Oct 21, 2006 5:29 pm
Location: Brisbane Australia
Contact:

Re: C char[] single character removal *SOLVED*

Post by B.E »

I hope your checking input to make sure it's length is 6+ because 5-6=-1 and 5-5 = 0, ie buffer overflow.

if you have not solve this problem yet. give you a clue.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STR "1234567890"

char * getstring(char * input){
	char *i, *j;
	int inputlen, k=0;

	i = j = input;
	inputlen = strlen(input);

	for(;*i;k++){
		*i = *j++;
		if ((k == inputlen-6) || (k == inputlen-5))
			continue;
		i++;
	}
	return input;
}

int main(){
	char *tmp = malloc(strlen(STR)+1);
	strcpy(tmp, STR);
	printf("%s\n", getstring(tmp));
        return 0;
}

NOTE if code looks like crap, it's because I haven't programmed in c for a while.
Image
Microsoft: "let everyone run after us. We'll just INNOV~1"
Post Reply