// Robert A Simpson

// cs132 C++

// Nov. 17,1997

// unit 6

 

 

// strings.cpp

#include <assert.h>

#include <iostream.h>

#include <time.h> // size_t is here

// prototypes

char * strcpy (char * s1, const char * s2);

char * strncpy (char * s1, const char * s2, size_t n);

char * strcat (char * s1, const char * s2);

char * strncat (char * s1, const char * s2, size_t n);

int strcmp (const char * s1, const char * s2);

int strncmp (const char * s1, const char * s2, size_t n);

char * strchr (char * s, int c);

size_t strlen (const char * s);

main()

{

// Test strlen.

{

assert (strlen ("abcd") == 4);

assert (strlen ("") == 0);

}

// Test strcmp.

{

assert (strcmp ("abcd", "abcd") == 0);

assert (strcmp ("abcd", "abce") < 0);

assert (strcmp ("abce", "abcd") > 0);

assert (strcmp ("ab", "abcd") < 0);

assert (strcmp ("abcd", "ab") > 0);

assert (strcmp ("", "") == 0);

assert (strcmp ("", " ") < 0);

assert (strcmp (" ", "") > 0);

}

/*

// Test strcpy.

{

char word1 [] = "abcdef";

char word2 [10];

assert (strcmp (word1, strcpy (word2, word1)) == 0);

assert (strcmp (word2, word1) == 0);

assert (strlen (word2) == 6);

assert (strlen (strcpy (word2, "")) == 0);

assert (strlen (word2) == 0);

}

// Test strncpy.

{

char word1 [] = "abcdef";

char word2 [] = "ghijkl";

assert (strcmp ("abcjkl", strncpy (word2, word1, 3)) == 0);

assert (strcmp ("abcjkl", word2) == 0);

assert (strcmp ("abcdef", word1) == 0);

assert (strlen (strncpy (word1, "", 1)) == 0);

}

*/

// Test strncmp.

{

char word1[10] = "abcdef";

char word2 [] = "abc";

assert (strncmp (word1, word1, 10) == 0);

assert (strncmp (word1, word2, 4) > 0);

assert (strncmp (word1, word2, 3) == 0);

}

/*

// Test strcat.

{

char word1 [10] = "abc";

char word2 [] = "abc";

assert (strcmp ("abcdef", strcat (word1, "def")) == 0);

assert (strcmp ("abcdef", word1) == 0);

assert (strcmp (word2, strcat (word2, "")) == 0);

}

// Test strncat.

{

char word1 [10] = "abc";

char word2 [10] = "abc";

assert (strcmp ("abcdef", strncat (word1, "def", 10)) == 0);

assert (strcmp ("abcdef", word1) == 0);

assert (strcmp ("abcde", strncat (word2, "def", 2)) == 0);

assert (strcmp ("abcde", word2) == 0);

}

// Test strchr.

{

char word1[] = "abcdcba";

assert (strcmp ("abcdcba", strchr (word1, 'a')) == 0);

assert (strcmp ("dcba", strchr (word1, 'd')) == 0);

assert (strcmp ("abcdcba", word1) == 0);

assert (strcmp ("", strchr (word1, '\0')) == 0);

assert (strchr (word1, 'z') == NULL);

}

*/

// If the program gets to here, all tests succeeded.

cout << "!!!!!!!! SUCCESS !!!!!!!!" << endl;

return 0;

}

/////////////////////////////////////////////////////////////////////////

/* strcpy: Copy the string pointed to by s2 (including the terminating

* null character) into the array pointed to by s1. If copying

* takes place between objects that overlap, the behavior is

* undefined.

* Return the value of s1.

*/

char * strcpy (char *s1, const char * s2)

{

while (*s2 != '\0'){

*s1=*s2;

s1++;

s2++;

}

return (s1);

}

/////////////////////////////////////////////////////////////////////////

/* strncpy: Copy not more than n characters (characters that follow a

* null character are not copied) from the array pointed to by

* s2 to the array pointed to by s1. If copying takes place

* between objects that overlap, the behavior is undefined.

* If the array pointed to by s2 is a string that is shorter

* than n characters, null characters are appended to the copy

* in the array pointed to by s1, until n characters in all have

* been written.

* Return the value of s1.

*/

char * strncpy (char * s1, const char *s2, size_t n)

{

int count;

for (count=0;count<=n;count++) {

s1++;

s2++;

}

for (count=0;count<=n;count++){

*s1=*s2;

s1++;

s2++;

}

return (s1);

}

/////////////////////////////////////////////////////////////////////////

/* strcat: Append a copy of the string pointed to by s2 (including

* the terminating null character) to the end of the string

* pointed to by s1. The initial character of s2 overwrites

* the null character at the end of s1. If copying takes place

* between objects that overlap, the behavior is undefined.

* Return the value of s1.

*/

char * strcat (char *s1, const char * s2)

{

int count=0;

while (*s1 !='\0'){

count++;

}

*s1=*s1+count;

while(*s2 !='\0'){

*s1=*s2;

}

return (s1);

}

/////////////////////////////////////////////////////////////////////////

/* strncat: Append not more than n characters (a null character and

* characters that follow it are not appended) from the array

* pointed to by s2 to the end of the string pointed to by s1.

* The initial character of s2 overwrites the null character

* at the end of s1. A terminating null character is always

* appended to the result. If copying takes place between

* objects that overlap, the behavior is undefined.

* Returns the value of s1.

*/

char * strncat (char * s1, const char *s2, size_t n)

{

while(*s1 !='\0'){

s1++;

}

int count;

for (count=0;count<=n;count++)

*s1=*s2;

return (s1);

}

/////////////////////////////////////////////////////////////////////////

/* strcmp: Compare the string pointed to by s1 to the string pointed to

* by s2.

* Return an integer greater than, equal to, or less than zero,

* accordingly, as the string pointed to by s1 is greater than,

* equal to, or less than the string pointed to by s2.

*/

int strcmp (const char * s1, const char * s2)

{

do{

if (*s1=='\0' || *s2=='\0')

return (*s1-*s2);

s1++;

s2++;

}while (*s1==*s2) ;

return (*s1-*s2);

}

/////////////////////////////////////////////////////////////////////////

/* strncmp: Compare not more than n characters (characters that follow

* a null character are not compared) from the array pointed

* to by s1 to the array pointed to by s2.

* Returns an integer greater than, equal to, or less than zero,

* accordingly as the possibly null-terminated array pointed to by

* s1 is greater than, equal to, or less than the possibly null-

* terminated array pointed to by s2.

*/

int strncmp (const char * s1, const char *s2, size_t n)

{

int sum=0,sum1=0,count=0;

for (;count<n;count++){

if (*s1 != '\0')

sum +=*s1;

if (*s2 != '\0')

sum1 +=*s2;

s1 ++;

s2 ++;

}

return (sum-sum1);

}

/////////////////////////////////////////////////////////////////////////

/* strchr: Locate the first occurrence of c (converted to a char) in the

* string pointed to by s. The terminating null character is

* considered to be part of the string.

* Return a pointer to the located character, or a null pointer if

* the character does not occur in the string.

*/

char * strchr (char *s, int c)

{

while (*s != '\0'){

if (*s=c)

return (s);

s++;

}

return ('\0');

}

/////////////////////////////////////////////////////////////////////////

/* strlen: Compute the length of the string pointed to by s.

* Return the number of characters that precede the

* terminating null character.

*/

size_t strlen (const char *s)

{

int count=0;

while (*s != '\0'){

s ++;

count++;

}

return count;

}

program list

Robert's Home Page