VB icon

Another String Replace Function

Email
Submitted on: 1/4/2015 1:32:00 PM
By: William Brendel (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 1799
 
     Finds and replaces a string with a given string. Returns the number of occurances found.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Another String Replace Function
//**************************************
#include <string.h>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Another String Replace Function
// Description:Finds and replaces a string with a given string. Returns the number of occurances found.
// By: William Brendel (from psc cd)
//**************************************

// ==============================================================================
// int ReplaceTerm(char *szSource, char *szDest, char *szTerm, char *szReplace)
//
// Replaces all occurances of szTerm in szSource with szReplace and stores the 
// result in szDest.
//
// szSource- Source string
// szDest - The buffer into which the finished string will be stored
// szTerm - The string to look for
// szReplace - What to replace all instances of szTerm with
//
// Returns the number of occurances replaced, -1 on error.
// ==============================================================================
int ReplaceTerm(char *szSource, char *szDest, char *szTerm, char *szReplace)
{
	// The current start position (where we will start looking for occurances of 
	// szTerm)
	char *szLast;
	// The position of the next occurance of szTerm
	char *szLocation;
	// The number of occurances replaced
	int nOccurances = 0;
	
	// Must be non-NULLs
	if (!szSource || !szDest) {
		return -1;
	}
	// Clear the destination buffer
	szDest[0] = NULL;
	// Start from the beginning
	szLast = szSource;
	// Find the first occurance of szTerm
	szLocation = strstr(szLast, szTerm);
	// Loop through all instances in szSource
	while (szLocation) {
		// Copy the text preceeding the current occurance or szTerm
		strncat(szDest, szLast, szLocation - szLast);
		// Append szReplace to the end of the destination buffer
		strcat(szDest, szReplace); 
		// Increase the starting point so we don't find the same occurance over
		// and over again
		szLast = szLocation + strlen(szTerm);
		// Find the next occurance if it exists
		szLocation = strstr(szLast, szTerm);
		// Increment the occurance count
		nOccurances++;
	}
	// Finally append anything after the last occurance to the destination buffer
	strcat(szDest, szLast);
	// Return the occurance count
	return nOccurances;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.