Header Files


#include <string.h>

Creating Strings


	static const char *my_define = "/etc/sysconfig/network";

	const char *mytext = "Hello World";

String Length


	int mytext_length = strlen(mytext);

Does String Contain


	if (strncmp(*++argv, "O_RDONLY", 8) == 0)
		open_mode |= O_RDONLY;

Copy String

The preferred way

	strncpy (string_destination, string_source, max_length);		//Note no null will be included if the string is >= max_length
The more dangerous way

	strcpy (string_destination, string_source);

Return A Char Array From A Function

You need to dynamically allocate your char array.

For C++:


char *recvmsg(){
   char* buffer = new char[1024];
   return buffer;
}

For C:


char *recvmsg(){
   char* buffer = malloc(1024);
   return buffer;
}

Without this dynamic allocation the variable will reside on the function's stack and will therefore be destroyed on exit. This is why you will typically get a warning if you do this. Allocating it on the heap prevents this, BUT you will have to be careful and free the memory once done with it via delete[].

Replace String With String


//************************************************
//************************************************
//********** REPLACE STRING WITH STRING **********
//************************************************
//************************************************
//Look for first occurrence of a string and replace it with a new string
//The new outputted string is stored in original_string
int string_replace_string_with_string (char *original_string, char *find_string, char *replace_with_string)
{
	char buffer[MAX_STRING_LENGTH];
	char *p;

	p = strstr(original_string, find_string);						//Find first occurrence of the string
	if (p)
	{
		strncpy(buffer, original_string, p - original_string);		//Copy characters from original_string start to find_string st$
		buffer[p - original_string] = '';
		snprintf(buffer + (p - original_string),
				(MAX_STRING_LENGTH - (int)(buffer + (p - original_string))),
				"%s%s",
				replace_with_string,
				p + strlen(find_string));

		strncpy(original_string, buffer, MAX_STRING_LENGTH);		//Copy back into the original string
		return(1);
	}
	return(0);
}
/* Using the function:
#define	MAX_STRING_LENGTH 	1024

char original_string[MAX_STRING_LENGTH] = {"There are many variations of passages of Lorem Ipsum available, but are the majority have suffered alteration in some form"};
char find_string[] = {"are"};
char replace_with_string[] = {"z"};

string_replace_string_with_string (original_string, find_string, replace_with_string, MAX_STRING_LENGTH);
printf("nnMODIFIED STRING:n%sn", original_string);
*/

Find String Between Strings


//*************************************************
//*************************************************
//********** FIND STRING BETWEEN STRINGS **********
//*************************************************
//*************************************************
//Looks for first occurrence of a string1 and string2 and outputs the string between them
int string_find_string_between_strings (char *source_string, char *find_string1, char *find_string2, char *result_string)
{
	char *p_1;
	char *p_2;

	//Default to a null output string
	*result_string = '';

	p_1 = strstr(source_string, find_string1);
	p_2 = strstr(source_string, find_string2);

	if (p_1 && p_2)
	{
		//Exclude the search strings from the result
		p_1 += strlen(find_string1);

		//Ensure they are the right way round
		if (p_2 < p_1)
			return(0);

		int length = (p_2 - p_1);
		if (length > (MAX_STRING_LENGTH - 1))
			length = (MAX_STRING_LENGTH - 1);

		//Ensure the final byte is null
		result_string[length]  = '';


		strncpy(result_string, p_1, length);		//Copy characters from original_string start to find_string st$

		return(1);
	}
	return(0);
}
/* Using the function:
	#define	MAX_STRING_LENGTH 	1024

	char original_string[] = {"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form"};
	char find_string1[] = {"are "};
	char find_string2[] = {" but"};
	char result_string[MAX_STRING_LENGTH];

	string_find_string_between_strings(original_string, find_string1, find_string2, result_string, MAX_STRING_LENGTH);
	printf("nFOUND STRING:n[%s]n", result_string);
 */

 

Get String Up To String


//*********************************************
//*********************************************
//********** GET STRING UP TO STRING **********
//*********************************************
//*********************************************
//Looks for first occurrence of a string and outputs the string up to that string
int string_get_string_up_to_string (char *source_string, char *find_string, char *result_string)
{
	char *p;

	//Default to a null output string
	*result_string = '';

	p = strstr(source_string, find_string);
	if (p)
	{
		int length = (p - source_string);
		if (length > (MAX_STRING_LENGTH - 1))
			length = (MAX_STRING_LENGTH - 1);

		//Ensure the final byte is null (won't be added by strncpy if resutlt > MAX_STRING_LENGTH)
		result_string[length]  = '';


		strncpy(result_string, source_string, length);		//Copy characters from original_string start to find_string st$

		return(1);
	}
	return(0);
}
/* Using the function:
#define	MAX_STRING_LENGTH 	1024

char source_string[] = {"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form"};
char find_string[] = {" but "};
char result_string[MAX_STRING_LENGTH];

	string_get_string_up_to_string (source_string, find_string, result_string);
	printf("nBefore string:n[%s]n", result_string);
*/

Get String After String


//*********************************************
//*********************************************
//********** GET STRING AFTER STRING **********
//*********************************************
//*********************************************
//Looks for first occurrence of a string and outputs the string after that string
int string_get_string_after_string (char *source_string, char *find_string, char *result_string)
{
	char *p;

	//Default to a null output string
	*result_string = '';

	p = strstr(source_string, find_string);
	if (p)
	{
		p += strlen(find_string);

		//Ensure the final byte is null (won't be added by strncpy if resutlt > MAX_STRING_LENGTH)
		result_string[(MAX_STRING_LENGTH - 1)]  = '';

		strncpy(result_string, p, (MAX_STRING_LENGTH - 1));		//Copy characters from original_string start to find_string st$

		return(1);
	}
	return(0);
}
/* Using the function:
#define	MAX_STRING_LENGTH 	1024

char source_string[] = {"There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form"};
char find_string[] = {" but "};
char result_string[MAX_STRING_LENGTH];

	string_get_string_after_string (source_string, find_string, result_string);
	printf("nAfter string:n[%s]n", result_string);
*/

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *