0% found this document useful (0 votes)
221 views

For PM Review

The document describes a Makefile and C code for testing custom string handling functions. It defines string length, copy, search, concatenate and compare functions in my_string.h and their implementations in my_string.c. It then tests these functions by compiling and running three programs (program1.c, program2.c and program3.c) that exercise the custom functions on sample strings and compare the results to standard library functions.

Uploaded by

muhammad atiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
221 views

For PM Review

The document describes a Makefile and C code for testing custom string handling functions. It defines string length, copy, search, concatenate and compare functions in my_string.h and their implementations in my_string.c. It then tests these functions by compiling and running three programs (program1.c, program2.c and program3.c) that exercise the custom functions on sample strings and compare the results to standard library functions.

Uploaded by

muhammad atiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

***************************************Make

File************************************************************

program1: program1.c
clang program1.c my_string.c -o program1

clean:
rm -rf *.o

clobber: clean
rm -rf program1

***************************************my_string.h*********************************
***************************

/* a typedef to save us from writing "unsigned long" */

typedef unsigned long size_t2 ;

/* functions to determine the length of a string */

size_t2 my_strlen (const char *str) ;


size_t2 my_strlen2 (const char *str) ;

/* functions to copy a string */

char* my_strcpy (char *dest, const char *src) ;


char* my_strcpy2 (char *dest, const char *src) ;

/* functions to search a string */

char* my_strchr (const char *str, int c) ;


char* my_strchr2 (const char *str, int c) ;

/* functions to concatenate string */

char* my_strcat (char *dest, const char *src) ;


char* my_strcat2 (char *dest, const char *src) ;

/* functions to compare two strings */

int my_strcmp (const char *str1, const char *str2) ;


int my_strcmp2 (const char *str1, const char *str2) ;

***************************************my_string.c*********************************
***************************

#include "my_string.h"

// strlen functions takes in a pointer to a string and return its length


//
size_t2 my_strlen (const char *str) {

size_t2 index = 0 ;
while (str[++index] != '\0');
return index;

}
size_t2 my_strlen2 (const char *str) {

const char* s;
for (s = str; *s; ++s) ;
return (s - str);

char* my_strcpy (char *dest, const char *src) {


int i;
for(i=0;src[i]!='\0';i++){
dest[i]=src[i];
}
dest[i]='\0';
return dest;
}

char* my_strcpy2 (char *dest, const char *src)


{
char* d = dest;
for (; *src; ++src,dest++) {
*dest = *src;
}
*dest = '\0';
return d;

char* my_strchr (const char *str, int c)


{
int i=0;
while(str[i] != '\0')
{
if(str[i]==c)
{
break;
}
i++;
}
return (char*)&str[i];

char *my_strchr2(const char *s, int c)


{
while(*s != c && *s != '\0') {
s++;
}
return *s == c ? (char*)s : 0;
}

char* my_strcat (char *str1, const char *str2)


{
int i=0,j=0;
while(str1[i]!='\0')
i++;

while(str2[j]!='\0')
{
str1[i] = str2[j];
i++;
j++;
}
str1[i]='\0';
return str1;
}

char* my_strcat2 (char *str1, const char *str2)


{
while(*str1)
str1++;

while(*str2)
{
*str1 = *str2;
str1++;
str2++;
}
*str1='\0';
return str1;
}

int my_strcmp (const char *str1, const char *str2)


{
int i;
for(i=0;str1[i]&&str2[i] !='\0';i++)
{
if(str1[i]!=str2[i])
{
return 1;
}

}
return 0;
}

int my_strcmp2 (const char *str1, const char *str2)


{
while(*str1 && *str2 != '\0')
{
if(*str1!=*str2)
{
return 1;
}
str1++;
str2++;

}
return 0;
}

***************************************program1.c**********************************
**************************

#include <stdio.h>
#include <string.h>
#include "my_string.h"

int main() {

// a string we will test with


char str1 [100] = "Baba" ;
char str2 [100] = "Adam" ;
char str3 [100] ;

// test of strlen() functions


printf ("sample string: \"%s\" \n", str2) ;
printf ("string's length: strlen() = %lu \n", strlen (str2)) ;
printf ("string's length: my_strlen() = %lu \n", my_strlen (str2)) ;
printf ("string's length: my_strlen2() = %lu \n", my_strlen2(str2)) ;

/* CIT 593 students: TODO: add code to test your my_string.h functions */

//test of strcpy() function


printf("\ntest of strcpy() function\n");
printf ("string's Copied: strcpy() = %s \n", strcpy(str3,str1)) ;
printf ("string's Copied: my_strcpy() = %s \n", my_strcpy(str3,str1)) ;
printf ("string's Copied: my_strcpy2() = %s \n", my_strcpy2(str3,str1)) ;

// test of strchr() function


printf("\ntest of strchr() function\n");
printf("character search strchr() : %s\n",strchr(str2,'d'));
printf("character search my_strchr() : %s\n",my_strchr(str2,'d'));
printf("character search my_strchr2() : %s\n",my_strchr2(str2,'d'));

// test of strcat() function


printf("\ntest of strcat() function\n");
strcat(str1, str2);
printf("string's concatination : strcat() = %s \n", str1);

// reset str1
strcpy(str1, "Baba");
my_strcat(str1,str2);
printf ("string's concatination : my_strcat() = %s \n",str1 );

// reset str1
strcpy(str1, "Baba");
my_strcat2(str1, str2);
printf ("string's concatination : my_strcat2() = %s \n",str1 );

// test of strcmp() functions


printf("\ntest of strcmp() function\n");
strcmp(str1, str2) == 0 ? printf("strings are matched :strcmp() \n") :
printf("strings are Different :strcmp() \n");
my_strcmp(str1, str2) == 0 ? printf("strings are matched :my_strcmp()\n") :
printf("strings are Different :my_strcmp() \n");
my_strcmp2(str1, str2) == 0 ? printf("strings are matched :my_strcmp2() \n")
: printf("strings are Different :my_strcmp2() \n");

return 0;
}

***************************************program2.c**********************************
**************************
#include <stdio.h>
#include <string.h>
#include "my_string.h"

int main() {

// a stirng to be tested
char str[100]="Hello World";
printf("original string : %s\n",str);

// test to strrev() function


printf("reversed string : %s\n",my_strrev(str));

// test to strccase() function


char* ptr=my_strccase(str);
printf("case modified string : %s\n",ptr);
return 0;
}

***************************************program3.c**********************************
**************************

#include <stdio.h>
#include <string.h>

int main (int argc, char** argv)


{
char info[250]="";
int arr[10];
int j = 0;
printf("# of arguments passed: %d\n", argc) ;
for (int i=1; i< argc ; i++)
{
int a;
int ret =sscanf(argv[i],"%d",&a);
if (ret == 1) {
arr[j] = a;
j++;
}
else
{
if(strlen(info) > 0) strcat(info, ",");
strcat(info, argv[i]);
}
}

printf("Integer Array: {");


for (int i=0; i< j ; i++)
{
printf ( "%d",arr[i] );
if(i+1 < j)
printf(",");
}
printf("}");
printf("\nString Args:%s", info);
return (0) ;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy