#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char *s1 = "ZBCDEF";
const char *s2 = "ABCDEFG";
/* strlen using compact pointer notation
*/
size_t MyStrlen(const char *s1)
{
const char *ptr = 0;
while(*s1)
{
*s1++;
++ptr;
}
return( (size_t)ptr );
}
/* String comparison using compact pointer notation
*/
int MyStrcmp(const char *s1, const char *s2)
{
while(*s1)
{
if (*s1 == *s2)
{
*s2++;
*s1++;
if (*s2 == 0 && *s1 == 0)
return(0);
else if (*s2 == 0 && !*s1 == 0)
return(1);
else if (!*s2 == 0 && *s1 == 0)
return(-1);
}
else if(*s1 > *s2)
{
return(1);
}
else if(*s2 > *s1)
{
return(-1);
}
}
return(0);
}
char *GetSubString(const char source[], int start, int count, char result[])
{
if ((unsigned int)start > strlen(source))
{
*result = 0;
}
else
{
if ( (unsigned int)start + count > strlen(source)) count = strlen(source) - start;
while(start > 0)
{
source++;
start--;
}
*(result + count) = 0;
while ( count > 0 || *(source + count) != 0 )
{
count--;
*(result + count) = *(source + count);
}
}
return(result);
}
#define RESULT_MAX 19
int main(void)
{
char result[19];
const char source[] = "JIBSON GLOMGOLDS";
printf("nMyStrlen: %inn",(int)MyStrlen(s1));
printf("MyStrcmp: %inn", MyStrcmp(s1,s2));
printf("GetSubString %snn", GetSubString(source,1,5,result));
return(0);
}
Fun with pointers, see if you can figure it out. By the time I finished this, I finally understood pointers.
C is a great language, try it today.