hi all ,
This is a question that I doing for fun , in the book Data Structures using C++
, The question is to to implement those functions that are already implemented in the standard c library.
(a) strlen (b) strcmp (c) strcmp (d) strchr
I have already write those things . And now I looking ideas for proper and very clean interface . I use a helder file , Question5.h .
1 2 3 4 5 6 7 8 9
|
namespace question5
{
int strlen ( const char * ); // this will output the length of the string via integer.....
bool strcmp ( const char * , const char * ) ; // This will compare two strings. If they are same this will return 1 otherwise 0
char * strcat ( char * , const char * ) ; // this copies the string second parameater to the second one
char * strchr ( char * , int ) ; // locate the first occurence of aa string character.
}
|
and it's implementation ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include "Question5.h"
int question5::strlen( const char * input)
{
int size = 0 ;
while ( * input != 0 )
{
input++ ;
size++ ;
}
return size ;
}
bool question5::strcmp ( const char * str1 , const char *str2 )
{
while ( *str1 != 0 ) {
if ( * str1 != *str2 )
return 0 ;
str1++ ;
str2++ ;
}
return 1;
}
char * question5::strcat ( char * str1 , const char * str2)
{
char * str1_original = str1 ;
// just we are overwriting that memory.....
while ( *str2 != 0 )
{
*str1 = *str2 ;
str1++ ;
str2++ ;
}
// just append that at last
//str1++ ;
*str1 = 0 ;
return str1_original ;
}
char * question5::strchr ( char * str , int ch)
{
// convert the integer to the char
char ch1 = (char) ch ;
// then find for that...
while ( *str != ch1 )
{
if ( *str == 0 )
return (char * ) 0;
str++ ;
}
return str;
}
|
and the problem is why should I have to use question5:: always ?
why that new #include syntax , I mean like
#include <iostream>
using namespace std ;
Like , not working for here .
I mean using like this .
#include <./Question5>
using namespace question5 ;
and any ideas to improve how to write a good interface ?