943,697 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 10498
  • C RSS
Sep 7th, 2003
0

Null Terminated Arrays

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blackspyder01 is offline Offline
2 posts
since Sep 2003
Sep 8th, 2003
0
Re: Null Terminated Arrays
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:

  1. register char *temp = array;
  2. while (*temp++ != character && *temp != 0);
  3. *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:

  1. register char *temp = array;
  2. while (*temp++!=0)
  3. 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.
Last edited by Mike29936; Sep 8th, 2003 at 12:19 am.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
Mike29936 is offline Offline
22 posts
since Sep 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Launching program from resources
Next Thread in C Forum Timeline: ATL Web Service





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC