main pgr
written by sekhar
written on 03/08/07 */
#include
#define size 40
int str_len(char *);
void str_cpy(char *,char *);
void str_cat(char *,char *);
void str_rev(char *);
int str_word_count(char *);
int str_cmp(char *,char *);
main()
{
int option,len,count,comp;
char str1[size],str2[size];
while(1)
{
puts("\n1:str_len 2:strcpy 3:strconcat 4:strrev 5:strwordcount 6:exit");
puts("\nenter the option:");
scanf("%d",&option);
switch(option)
{
case 1:
puts("enter the string:");
__fpurge(stdin);
gets(str1);
len=str_len(str1);
printf("%d",len);
break;
case 2:
puts("enter the source sting to copy");
__fpurge(stdin);
gets(str1);
str_cpy(str1,str2); //str1 is source str2 is destination
puts(str2);
break;
case 3:
puts("enter the first string:");
__fpurge(stdin);
gets(str1);
puts("enter the second string:");
__fpurge(stdin);
gets(str2);
str_cat(str1,str2); //str1 & str2 is concate nate result in str1
puts(str1);
break;
case 4:
puts("enter the string foe reversing");
__fpurge(stdin);
gets(str1);
str_rev(str1);
puts(str1);
break;
case 5:
puts("enter the string for word count");
__fpurge(stdin);
gets(str1);
count=str_word_count(str1);
printf("%d",count);
break;
case 6:
puts("enter the first string");
__fpurge(stdin);
gets(str1);
puts("enter the second string");
__fpurge(stdin);
gets(str2);
comp=str_cmp(str1,str2);
printf("%d",comp);
break;
case 7:
exit();
}
}
}
/* function for string concatination
two string concatenate and result will be stored first string
written by sekhar
written on 03/08/07
modified on 03/08/07 */
void str_cat(char *s1,char *s2) //concatenate and store is s1
{
int y;
y=str_len(s1);//copy function calling source and destination is same
str_cpy(s2,s1+y);//same as above
}
/* function to compare two strings
written by sekhar
written on 03/08/07
modified on 03/08/07 */
int str_cmp(char *s1,char *s2)
{
int value=0,i;
while(*++s1!='\0'&&*++s2!='\0')
{
value=*s1-*s2;
}
if(value>0)
i=1;
if(value<0)
i=-1;
if(value==0)
i=0;
return i;
}
/* pgr for function of string copy
first string is source string second is destination string
written by sekhar
written on 03/08/07 */
void str_cpy(char *source, char *desti)
{
while(*source!='\0')
*desti++=*source++;
*desti='\0';
}
/*function to find str length
written by sekhar
written on 03/08/07
modified on 03/08/07
*/
int str_len(char * s1)
{
int count=0;
while(*s1++!='\0')
{
count++;
}
return count;
}
/* function for reversing the sting
written by sekhar
written on 03/07/08
*/
void str_rev(char *s1)
{
char len,i=0;
char s2[40];
str_cpy(s1,s2);
len=str_len(s1);
for(;len>0;len,i)
{
s1[--len]=*(s2+i);
i++;
}
// s1[len]='\0';
}
/* pgr to count the no words in the sting
written by sekhar
written on 03/07/08
modified on 03/07/08*/
int str_word_count(char *s1)
{
int cou=1;
while(*s1++!='\0')
{
if(*s1==' ')
cou++;
}
return cou;
}
strout: main.o strlen.o strcpy.o strcat.o strrev.o strwordcount.o strcmp.o
$ gcc -o strout main.o strlen.o strcpy.o strcat.o strrev.o strwordcount.o strcmp.o
main.o: main.c
$ gcc -c main.c
strlen.o: strlen.c
$ gcc -c strlen.c
strcpy.o: strcpy.c
$ gcc -c strcpy.c
strcat.o: strcat.c
$ gcc -c strcat.c
strrev.o: strrev.c
$ gcc -c strrev.c
strwordcount: strwordcount.c
$ gcc -c strwordcount.c
strcmp: strcmp.c
$ gcc -c strcmp.c
clean:
$ rm -rf *.o

No comments:
Post a Comment