Pages

Custom Search
Showing posts with label programs. Show all posts
Showing posts with label programs. Show all posts

Saturday, August 9, 2014

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:  }  


Tuesday, October 11, 2011

Program to generate superprime numbers in a given range.


//Program to generate superprimes in a given range and display the number of superprimes.
//leave space between lower limit and upper limit

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

void superprime();                   //Function declaration

int main()
{
int  sn, en;
printf("\nEnter range of numbers: ");
scanf("%d %d", &sn, &en);

superprime(sn, en);                 //Function call

getch();
}
void superprime(int sn, int en)
{
int a, digit, d=2, flag=1, f=1, t, i, c=0;;

for(i=sn;i<=en;i++)
{
t=i;
flag=1;
d=2;
while(d<t)
{
if((t%d)==0)
{
flag=0;
break;
}
d++;
}
if(flag==1)
{
while(t>0)
{
a=2;
digit=t%10;
f=1;
while(a<digit)
{
if((digit%a)==0)
{
f=0;
break;
}
a++;
}
t=t/10;
if(f==0)
break;
}
if(f==1)
{
printf("%d\t", i);
c++;
}
}
}
printf("\n\nNumber of superprimes: %d\n", c);
}

SAMPLE OUTPUT:



Program to generate armstrong numbers in a given range.


//Generate armstrong number in a given range.
Leave space between the upper and the lower range.

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

void range_arm();                    //Function declaration

int main()
{
        int sn, en;
        printf("\nEnter range of numbers: ");
        scanf("%d %d", &sn, &en);
        range_arm(sn, en);                   //Function call
        getch();
}
void range_arm(int sn, int en)
{
        int t, cube, scube=0, d, i, c=0;

        for(i=sn;i<=en;i++)
        {
                scube=0;
                t=i;
                while(t>0)
                {
                        d=t%10;
                        cube=d*d*d;
                        scube=scube+cube;
                        t=t/10;
                }
                 if(scube==i)
                {
                        printf("%d\t", i);
                        c++;
                }
        }
        if(c==0)
        {
                printf("\nNo armstrong numbers in the given range");
        }
        printf("\n");
}

SAMPLE OUTPUT:


Custom Search