The Open Source Swiss Army Knife

/code/cPP/basics_cPP/
/code/cPP/basics_cPP/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /cPP   /basics_cPP 
Permalink: dastring.cpp
Title: add
article options : please login   |  raw source view  

// Author: joe speigle
// Summary: string functions

// System header files
#include <iostream>
//
// C-string Function Prototypes
int daStrLen(const char []); 
void daStrCpy(char [], const char []); 
void daStrCpyPtr ( char*, char*);
bool daStrEqual(const char * const, const char * const); 
void daStrCat(char [], const char []); 
int daStrCmp(const char * const, const char * const);

// prototypes for other local functions, as needed

// control function
int main()
{
	char test[20]= "test string";
	char src[80] = "this is a test string";
	char source_part[100] = "this is the beginning... " ;
	const char * append_part = " & this is the end. " ;
	const char * one = "sameness test string";
	const char * two = one;
	const char * one_diff = "same but different test string";
	char buffer[80] = "\0";
	int len = 0;
	bool tf;
	len = daStrLen(test);
	daStrCpy (buffer, src);
	tf =  daStrEqual(one, two);
	tf =  daStrEqual(one_diff, two);
	daStrCat(source_part, append_part);
	len = daStrCmp(one,two);
	len = daStrCmp(one,one_diff);
	return 0;
}

// other local functions, as needed


// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Use subscripts.  Copy the second string into the first one, replacing the value in the first
// (assume that the first has appropriate space)
void daStrCpy(char buffer[], const char str[]) 
{
 	std::cout << "testing StrCpy " << std::endl;
	std::cout << "\tbefore StrCpy: " << buffer << std::endl;
	int ctr = 0;
	for (; str[ctr] != '\0'; ctr++) {
		buffer[ctr] = str[ctr];		
	}
	buffer[ctr] = '\0';
	std::cout << "\tafter StrCpy:  " << buffer << std::endl;
}

// Use pointer arithmetic (no subscripts).  
// Return true if strings are the same, false if different.
// Two strings are the same if they have the same ASCII characters in the same order.
bool daStrEqual(const char *one, const char * two)
{
	std::cout << "testing daStrEqual with \"" << one << "\" and \"" << two << "\"" << std::endl;
	while (*one || *two)  {
		if (*one != *two) {
			std::cout << "\tare not the same" << std::endl;
			return 0;
		}
		*two++;
		*one++;
	}
	std::cout << "\tare the same" << std::endl;
	return 1;
}

// Use subscripts.  Concatenate (append) the second string onto the end of the first one 
// (assume that the first has appropriate space)
void daStrCat(char dest[], const char src[]) 
{
	std::cout << "testing daStrCat with \"" << dest << "\" and \"" << src << "\"" << std::endl;

	int len = 0, ctr = 0;
	while (dest[len] != '\0')  {
		len++;
	}
	for (;	src[ctr] != '\0'; ctr++, len++) {
		dest[len] = src[ctr];
	}
	dest[++len] = '\0'; 
	std::cout << "\tresult: \"" << dest << "\"" << std::endl;
}

// Use pointer arithmetic (no subscripts).  
// Compare two strings: return 0 if strings are the same, 
// return negative value if first is smaller, return positive value if first is larger.
// Larger/smaller is based on alphabetical order (by ASCII value of character) 
// not length of string.
int daStrCmp(const char * string_a, const char * string_b)
{
	std::cout << "testing daStrCmp with \"" << string_a << "\" and \"" << string_b << "\"" << std::endl;
	while (*string_a || *string_b)  {
		if (*string_a > *string_b) {
			std::cout << "\tstring_a is bigger" << std::endl;
			return -1;
		}
		if (*string_b > *string_a) {
			std::cout << "\tstring_b is bigger" << std::endl;
			return 1;
		}
		*string_b++;
		*string_a++;
	}
	std::cout << "\tstrings are the same" << std::endl;
	return 0;

}



Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top