954,170 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Null Terminated Arrays

I was wanting to know what is the best way to go about finding a char inside a char array and then replacing the char and outputing the changed array. Thanks Alot

blackspyder01
Newbie Poster
2 posts since Sep 2003
Reputation Points: 10
Solved Threads: 0
 

Do you know the position of the char?

If so:

char_array_name[position] = new_character;

char_array_name is the name of the array.
position is an integer value specifying the offset from the address of the start of the array.
new_character is the character you want it to replace it with, like 'A'.

If you don't know it:

register char *temp = array;
while (*temp++ != character && *temp != 0);
*temp = new_character;


temp is a pointer to the array
array is the name of the array
character is the numberical value of the character you want to find
new_character is the character you want it to change to

It'll search the array, and replace the first instance of the character you want changed to the character you want it to be.

If you want to search the whole array:

register char *temp = array;
while (*temp++!=0)
if (*temp == character) *temp = new_character


If you want to output it, just go:

printf("%s",array);

where array is the name of the array.

Alternatively, you can go with 'cout', but I like printf() better, I guess since C is the first language I learned.

Mike29936
Newbie Poster
22 posts since Sep 2003
Reputation Points: 12
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You