VB icon

String Functions

Email
Submitted on: 1/1/2015 1:34:00 PM
By: Faghmie J Davids (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++
Views: 781
 
     This header file contains functions to: Return the n number of characters from the left/right of a character string, remove trailing and leading spaces and replace a character with a string that is specified. The functions are pretty straight forward.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :String Functions
//**************************************
string.h, math.h
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: String Functions
// Description:This header file contains functions to: Return the n number of characters from the left/right of a character string, remove trailing and leading spaces and replace a character with a string that is specified. The functions are pretty straight forward.
// By: Faghmie J Davids (from psc cd)
//
// Returns:Each function returns a pointer to the return string
//
// Side Effects:None that I know of.
//**************************************

#include <string.h>
#include <math.h>
char * strLeft(char * str,int n)
{
	char * tmp;
	tmp = strdup(str);
	strncpy(tmp + n,"",abs(strlen(tmp) - n));
	
	return tmp;
}
char * strRight(char * str, int n)
{
	char * tmp;
	tmp = strdup(str);
	strrev(tmp);
	strncpy(tmp + n,"",strlen(tmp) - n);
	strrev(tmp);
	return tmp;
}
char * strTrim(char * str)
{
	char * tmp;
	tmp = strdup(str);
	//REMOVE LEADING SPACES
	while (strcmp(strLeft(tmp,1)," ") == 0)
	{
		tmp = strRight(tmp,strlen(tmp) - 1);
	}
	//REMOVE TRAILING SPACES
	while (strcmp(strRight(tmp,1)," ") == 0)
	{
		tmp = strLeft(tmp,strlen(tmp) - 1);
	}
	return tmp;
}
char * strReplace(char * str,char FindX, char * ReplaceX)
{
	char * tmp, * tmp2, * tmp3;
	tmp = strdup(str);
	tmp2 = strchr(tmp,FindX);
	while (tmp2 != NULL)
	{
		tmp3 = strLeft(tmp,strlen(tmp)-strlen(tmp2));
		tmp2++;
		tmp = strdup(tmp3);
		strcat(tmp,ReplaceX);
		strcat(tmp,tmp2);
		tmp2 = strchr(tmp,FindX);
	}
	return tmp;
}


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 Intermediate 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.