Parse a String in C


Using just C

I would like to parse a string and:

  1. count the occurrences of a character in a string (for example, count all the 'e's in a passed in string)
  2. Once counted (or even as I am counting) replace the e's with 3's

Best Solution

OK, you're either lazy, or stuck, assuming stuck.

You need a function with a signature something like

int ReplaceCharInString(char* string, char charToFind, char charThatReplaces)


Inside the function you need

  1. To declare an integer to count the occurrences
  2. A loop that moves from the start of the string to it's end
  3. inside the loop, an if statement to check is the current char the charToFind,
  4. statements to increment the count of occurrences and perform the replacement
  5. After the loop, you need to return the count of occurrences