Thursday, June 9, 2011

User defined C Program for String Functions!


   // Program for Finding String Length without String Function
  
 #include <string.h>
 #include <conio.h>
 #include <stdio.h>

 void main()
  {
     int i,j,len=0;
     char str[50];
     clrscr();
     printf("\nEnter a String : " );
     gets(str);
     for(i=0;str[i]!='\0';i++)
   {
      len++;
}
printf("\n============ String Length ========== \n");
printf("\n String  '%s' and  the Length is %d ",str,len);
getch();
}

Output :
  
Enter a String : this is a test length
  
============ String Length ==========
  
 String  'this is a test length' and  the Length is 21

  
// Program for Copy a String to another without using String Function
     
 #include <string.h>
 #include <conio.h>
 #include <stdio.h>
      void main()
          {
               int i,j,len=0;
               char src[50],dest[50];
               clrscr();
               printf("\nEnter a the Source String : " );
               gets(src);
               for(i=0;src[i]!='\0';i++)
                   {
                        dest[i]=src[i];
                   }
               dest[i]='\0';
               printf("\n============ String Copy ========== \n");
               printf("\n Source String  '%s' and  Copied to '%s' ",src,dest);
               free(src);
               getch();
      }
  

Output:
     
Enter a the Source String : Creating Function

============ String Copy ==========

 Source String  'Creating Function' and  Copied to 'Creating Function'
  

  
// Program for Compare two String without using String Function
  
 #include <string.h>
 #include <conio.h>
 #include <stdio.h>

      void main()
          {
               int i,flag=0;
               char str[50],str1[50],ch,ch1;
               clrscr();
               printf("\nEnter a the First String : " );
               gets(str);
               printf("\nEnter a the Second String : " );
               gets(str1);
               for(i=0;str[i]!='\0';i++)
                   {
                        ch=str[i];
                        ch1=str1[i];
                        if(ch>=97 && ch<=122)
                             {
                                    ch-=32;
                             }
                        if(ch1>=97 && ch1<=122)
                             {
                                    ch1-=32;
                              }
                        if(ch!=ch1)
                             {
                                    flag=1;
                                    break;
                             }
                   }
               printf("\n============ String Compare ========== \n");
               if(flag==0)
                   {
                        printf("\n Both  String  '%s' and  '%s' are equal ",str,str1);
                   }
               else
                   {
                        printf("\n Both  String  '%s' and  '%s' are Not equal ",str,str1);
                   }
               getch();
      }

Output:
  
Enter a the First String : Testing
Enter a the Second String : testing
  
============ String Copy ==========
  
 Both  String  'Testing' and  'testing' are equal

Enter a the First String : Test
Enter a the Second String : Rest
  
============ String Copy ==========
  
 Both  String  'Test' and  'Rest' are Not equal

Enter a the First String : TEST
Enter a the Second String : test
  
============ String Copy ==========
  
 Both  String  'TEST' and  'test' are equal
  

  
// Program for String Reverse without String Function
  
 #include <string.h>
 #include <conio.h>
 #include <stdio.h>
      
      void main()
          {  
               int i,j,len=0;
               char str[50],revstr[50];
               clrscr();
               printf("\nEnter a String : " );
               gets(str);
               for(i=0;str[i]!='\0';i++)
                   {
                        len++;
                   }

               j=0;
               for(i=len-1;i>=0;i--)
                   {
                        revstr[j++]=str[i];
                   }
          printf("\n Original String  '%s' and  Reversed to '%s' ",str,revstr);
          free(str);      //  Releases memory block occupied by str
          getch();
}

Output:
  
Enter a String : Hello World!
  
============ String Reverse ==========
  
 Original String  'Hello World!' and  Reversed to '!dlroW olleH'

  
// Program for String Concatenation without String Function
  
 #include <string.h>
 #include <conio.h>
 #include <stdio.h>
  
      void main()
          {
               int i,len=0,j;
               char str[50],str1[50],ch,ch1;
               clrscr();
               printf("\nEnter a the First String : " );
               gets(str);
               printf("\nEnter a the Second String : " );
               gets(str1);
               for(i=0;str[i[!='\0';i++)
                   {
                         len++;
                   }
               len=len-1;
               for(i=0;str1[i]!='\0';i++)
                   {
                        str[len++]=str1[i];
                   }
               str[len]='\0';
               printf("\n============ String Concatenation ========== \n");
               printf("\n First  String  '%s' and  Second String '%s'  ",str,str1);
               printf("\n Concatenated in First '%s' ",str);
               getch();
      }
  

   Output:
  
Enter a the First String : Microsoft
  
Enter a the Second String : Office
  
============ String Concatenation ==========
  
 First  String  'Microsoft' and  Second String 'Office'
 Concatenated in First 'MicrosoftOffice'

  
// Program for StringTokenizer  without String Function

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

      void main()
          {
               int i,len=0,j;
               char str[100],str1[50],ch,ch1;
               clrscr();
               printf("\nEnter a Sentence : " );

               gets(str);
               printf("\n============ String Tokenizer ========== \n");
               printf("\n Origina Input '%s' the Tokens : ",str);
               for(i=0;str[i]!='\0';i++)
                   {
                        len++;
                   }
               str[len]=' ';
               str[len+1]='\0';
               for(i=0;i<len;i++)
                   {
                        for(j=0;j<50;j++)
                             {
                                    str1[j]='\0';
                             }
                        j=0;
                        while(str[i]!=' ')
                             {
                                    str1[j++]=str[i];
                                    i++;
                             }
                          printf("\n%s",str1);
                   }
               getch();
      }
  

Output:
     
Enter a Sentence : I have a dream
  
============ String Tokenizer ==========
  
   Original Input 'I have a dream' the Tokens :
I
have
a
dream

No comments: