Saturday, 8 November 2014

C programs on Strings.



              Level 1

1) Source Code to Calculated Length without Using strlen() Function


     #include<conio.h>
     #include<stdio.h>
     void main()
    {
       int l;        //Variable used to calculate Lenght
      char string[50];    //Declared a String.
      
      clrscr();     //For Clearing the Screen.
      
      printf("Enter the String : "); //For printing
      gets(string);   //For Input.

     for(l=0;string[l]!=NULL;l++) //For loop
      {}                          //it will increment the value of 'l' till the NUll is repoter in string 
     
      printf("\nLenght is : %d",i ); //Printing 
      getch();    //Getting a chracter or waiting for key press.
    }




2) C Program to Reverse a String by Passing it to Function

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

     void length(char *a)
     {
       int i,l;
     char temp[50];  //temporary String

        for(i=0;a[i]!=NULL;i++) //Calculating Sting length

         {}

        l=i-1;


      for(i=0;i<a[i]!=NULL;i++,l--)     //Logic

       {temp[l]=a[i];
       }
      printf("Reverse of String is %s",temp); //Printing Reverse String
    }

   void main()
   {
     char string[50];
     clrscr();    // Clearing the Screen.
     printf("Enter the String to Reverse it :");  //Printing
     gets(string);         //Input
     length(&string);      //Calling Function Length
     getch();
   }