Write a program that does not use the inbuilt string functions to perform the following: 10m Jun2006

By | June 14, 2014

Write a program that does not use the inbuilt string functions to perform the following: 10m Jun2006

(i) To compare two strings

#include<stdio.h>
void main()
{
 int str_cmp(char*,char*);
 char Str1[50],Str2[50];
 int val;
 clrscr();
 printf("\nENTER FIRST STRING TO COMPARE : ");
 scanf("%s",&Str1);
 printf("\nENTER SECOND STRING TO COMPARE : ");
 scanf("%s",&Str2);
 val=str_cmp(Str1,Str2);
 if(val==0)
 printf("\nSTR1 IS EQUAL TO STR2");
 else if(val<0)
 printf("\nSTR1 IS LESS TO STR2");
 else if(val>0)
 printf("\nSTR1 IS GREATER TO STR2");
 getch();
}
int str_cmp(char *STR1,char *STR2)
{
   int I,S1=0,S2=0,val=0,L1=0,L2=0;
   {
     while(*STR1)
     {
   L1++;
   S1+=*STR1;
   STR1++;
     }
     while(*STR2)
     {
   L2++;
   S2+=*STR2;
   STR2++;
     }
     if(L1==L2)
     {
 val=S1-S2;
 printf("\n%d",val);
     }
     else if(L1<L2)
 val=-1;
     else if(L1>L2)
 val=1;
   }
  return val;
}

Screen Shots:

C_program_String_Compare

C_program_String_Compare_Output

(ii) To copy a string

#include<stdio.h>
void main()
{
 void str_copy(char*,char*);
 char Str1[50],Str2[50];
 int val;
 clrscr();
 printf(“\nENTER FIRST STRING TO COMPARE : “);
 scanf(“%s”,&Str1);
 str_copy(Str2,Str1);
 printf(“\nSTR1 IS : %s”,Str1);
 printf(“\nSTR2 IS : %s”,Str2);
 getch();
}
void str_copy(char *STR2,char *STR1)
{
    while(*STR1)
     {
   *STR2=*STR1;
   STR1++;
   STR2++;
     }
     *STR2=*STR1;

}

#include<stdio.h>
void main()
{
 void str_copy(char*,char*);
 char Str1[50],Str2[50];
 int val;
 clrscr();
 printf("\nENTER FIRST STRING TO COMPARE : ");
 scanf("%s",&Str1);
 str_copy(Str2,Str1);
 printf("\nSTR1 IS : %s",Str1);
 printf("\nSTR2 IS : %s",Str2);
 getch();
}
void str_copy(char *STR2,char *STR1)
{
    while(*STR1)
     {
   *STR2=*STR1;
   STR1++;
   STR2++;
     }
     *STR2=*STR1;
}
 

Screen Shots:

C_program_String_Copy

C_program_String_Copy_Output

Design an algorithm, and write a program to find the factorial of a number using recursion. 10m Jun2006

 Algorithm:

 int fact(int NUM)
  {  
     int RES;
      if(NUM==1)
          return(1);
      else
          RES=NUM*fact(NUM-1);
   return(RES);
  }

Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-to-find-the-factorial-of-a-number-using-recursion

Leave a Reply