Pages

Custom Search

Saturday, August 9, 2014

Program to convert string to title case

Program to convert string to title case.
In this program we are going to convert this first letter of every word in the string to UPPER CASE.
and all other letters to lower case.
This is simple but interesting program.

The LOGIC part:
Logic of the program is very simple.
Character by character parse the string from start to end of string.
Check if each letter is in proper case. If not convert it to the proper case.
SOURCE CODE:
 #include <stdio.h>  
 #include <stdlib.h>  
 int main()  
 {  
      char str[50];  
      int i, j, len;  
      printf("\nEnter a string: ");  
      gets(str);  
      printf("\nEntered string: ");  
      puts(str);  
      len=strlen(str);  
      i=0;  
      while(i!=len)                                   //til end of string  
      {  
           if((i==0))                                   //first letter of string  
           {  
                if(str[i]>90)                         //if it is not UPPER case  
                     str[i] = str[i]%65+33;  
                i++;  
           }  
           else if((str[i]!=' ') && (str[i]<=90))          //any other position than first letter of word  
           {  
                str[i]=str[i]%97+32;  
                i++;  
           }  
           else if(str[i]==' ')                    //for first letter of a word  
           {  
                i++;  
                if(str[i]>96)  
                     str[i]=str[i]%65+33;  
                i++;  
           }  
           else  
                i++;                                   //if letter is in correct case, simply increment i  
      }  
      str[i]='\0';  
      printf("\nOutput: ");  
      puts(str);  
      printf("\n\n");  
      return 1;  
 }  


SAMPLE OUTPUT

Program to reverse the order of words in a string

Program to reverse the order of words in a string.
For Eg:
Input: this is a simple c program
Output: program c simple a is this

The LOGIC part:
-First determine the length of the string. Use a simple for loop
until you find '\0' else use an inbuilt string function 'strlen()' like I have used.
-then move over to the last word in your string.
Place a pointer at the end of the last word and then find the
space before that word and place a pointer after the space
and using these two pointer append the word to a new string.
-Repeat procedure for all words from last word to first word.


SOURCE CODE:

 #include <stdio.h>  
 #include <stdlib.h>  
 int main()  
 {  
      char str[100], rev[100];  
      int i, j, k, m, n, len, c=0, flag=1;  
      printf("\nEnter a sentence: ");  
      gets(str);  
      printf("\nEntered sentence: ");  
      puts(str);  
      len = strlen(str)-1;          //length of string except \0  
      i=0;  
      j=len;  
      k=0;  
      while(flag==1)                    //while more words to process  
      {  
           flag = 1;  
           i=j;                         //pointer i, j to end of current word  
           while(str[i] != ' ')     //decrement pointer i till beginning of current word  
           {  
                if(i==0)  
                {  
                     flag = 0;          //special case for 1st word of the sentence  
                     break;  
                }  
                i--;  
           }  
            m=i;  
           if(flag)  
           {  
                i=i+1;       
           }       
           for( ; i<=j; )               //append current word to new char array  
           {  
                rev[k++] = str[i++];  
           }  
           j=m-1;                         //move pointer j to end of previous word  
           rev[k++]=' ';  
      }  
      rev[k]='\0';  
      printf("\nReverse sentence: ");  
      puts(rev);  
      printf("\n");  
      return 1;  
 }  


SAMPLE OUTPUT:

Thursday, October 17, 2013

Reversing array contents in the same array.

Reversing array contents:

In this program, we have to reverse the array contents without using any temporary array.
This can be achieved using swapping technique.
Have one index 'i' pointing to first element and one index 'j' pointing to last element.
Swap their contents.
Increment i, decrement j
Continue as long as i is less than or equal to j.
Done.
Print the array contents again, which are now in reverse order.


Source Code:



1:  #include <stdio.h>  
2:  #include <stdlib.h>  
3:    
4:  int main()  
5:  {  
6:       int i, a[10], t, n, j;  
7:         
8:       printf("\nEnter the size of the array: ");  
9:       scanf("%d", &n);  
10:         
11:       printf("\nEnter the array elements: ");  
12:       for(i=0;i<n;i++)  
13:            scanf("%d", &a[i]);  
14:              
15:       for(i=0,j=n-1;i<=j;i++,j--)  
16:       {  
17:            t=a[i];  
18:            a[i]=a[j];  
19:            a[j]=t;  
20:       }  
21:         
22:       printf("\nThe reverse of the array is: ");  
23:       for(i=0;i<n;i++)  
24:            printf("%d ", a[i]);  
25:       printf("\n");  
26:         
27:       return 1;  
28:  }  


Friday, March 23, 2012

Quicksort to sort elements in ascending order using the first element as the Pivot.



Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.
The steps are:
    -Pick an element, called a pivot, from the list.[Fisrt element in this program.]
    -Reorder the list so that all elements with values less than the pivot come before the pivot,      while all elements with values greater than the pivot come after it (equal values can go             either way).
      After this partitioning, the pivot is in its final position. This is called the partition operation.
    -Recursively sort the sub-list of lesser elements and the sub-list of greater elements.



Source Code:




 #include <stdio.h>  
 #include <stdlib.h>  
 int partition(int a[], int l, int r)  
 {  
     int p, i, j, t;  
     p = l;  
     i = l;  
     j = r;  
     while(i<j)  
     {  
          while((a[ i ] <= a[ p ]) && (i<r))  
          {  
             i++;  
          }  
          while(a[ j ] > a[ p ])  
          {  
             j--;  
         }  
         if(i<j)  
         {  
             t=a[ i ];  
             a[ i ] = a[ j ];  
             a[ j ] = t;  
         }  
     }  
     t=a[ p ];  
     a[ p ] = a[ j ];  
     a[ j ] = t;  
     return j;  
 }  
 void quicksort(int a[50], int l, int r)  
 {  
     int s;  
     if(l < r)  
     {  
         s=partition(a,l,r);  
         quicksort(a, l, s-1);  
         quicksort(a, s+1, r);  
     }  
 }  
 int main()  
 {  
     int a[50], l, r, i, n;  
     printf("\nEnter the size of the array: ");  
     scanf("%d", &n);  
     srand(time(NULL));  
     for(i=0;i<n;i++)  
     {  
         a[ i ] = rand()%50 + 1;  
     }  
     printf("\nThe elements of the array before sort are: \n");  
     for(i=0;i<n;i++)  
     {  
         printf("%d ", a[ i ]);  
     }  
     quicksort(a,0,n-1);  
     printf("\nThe elements of the array after sort are: \n");  
     for(i=0;i<n;i++)  
     {  
         printf("%d ", a[ i ]);  
     }  
     getch();  
 }  
Sample Output:





Wednesday, February 29, 2012

Merge-sort for sorting elements in ascending order.

Merge-sort:
It is a sorting technique that sequences data by continuously merging items in the list. Every single item in the original unordered list is merged with another, creating groups of two. Every two-item group is merged, creating groups of four and so on until there is one ordered list.


Source Code:

 #include <stdio.h>  
 #include <stdlib.h>  
 int c[50];  
 void merge(int b[], int low,int mid,int high)  
 {  
     int i, j, k=low;  
     i = low;  
     j = mid+1;  
     while((i <= mid) && (j <= high))   //Comparing & inserting items in new ordered list  
     {  
         if(b[i] <= b[j])  
             c[k++] = b[i++];  
         else  
             c[k++] = b[j++];  
     }  
     while(i <= mid)        //Inserting remaining items from 1st half in new list  
         c[k++] = b[i++];  
     while(j <= high)       //Inserting remaining items from 2nd half in new list  
         c[k++] = b[j++];  
     for(k=low;k<=high;k++)  
         b[k] = c[k];  
 }  
 void mergesort(int a[], int low, int high)  
 {  
     int mid;  
     if(low<high)  
     {  
         mid = (low+high)/2;  
         mergesort(a,low,mid);           //Dividing the list into two - 1st half  
         mergesort(a,mid+1,high);         // 2nd half  
         merge(a,low,mid,high);          //Merging the lists  
     }  
 }  
 int main()  
 {  
     int a[50], i, n;  
     double e,s,t;  
     printf("\nEnter the size of the array: ");  
     scanf("%d", &n);  
     srand(time(NULL));  
     for(i=1;i<=n;i++)  
         a[i] = rand()%50 +1;  
     printf("\nElements of the array are: ");  
     for(i=1;i<=n;i++)  
         printf("%d ", a[i]);  
     mergesort(a,1,n);  
     printf("\nThe sorted array is: ");  
     for(i=1;i<=n;i++)  
         printf("%d ", c[i]);  
 }  



Sample Output:



Custom Search