Saturday 15 October 2016

C program | Use of Pointers, functions & Unusual use of Switch Case

C program | Use of Pointers, functions, Unusual use of Switch Case

 

What is this c program about? 

This program scans number of words from user in range of 1 to 10.  Then user will enter those many number of words in form of a string one by one, each word should be scanned in a new line. Only after scanning all the words from user, each word is processed. Processing of word is done as >> Letters at even places are printed first followed by 2 blank spaces and then letters at odd places of the word are printed at last for each word. 

Length of the word entered by the user should be long enough. This program can take a word of 97 characters long.
  •    sample input:
   2               (number of words scanned)
   Hello         (1st word entered by user)
   Vishal        (2nd word entered by user)
  •    output :
   Hlo  el       (output of Hello word)
   Vsa  ihl      (output of Vishal)


C program: 

 

#include<stdio.h>
#include<conio.h>
char* processing(char* );
void main()
{

 int i,n;
 char a1[100];
 char *p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9,*p10;
 clrscr();

 printf("Enter no. of words, mimimum -> 1 maximum -> 10 \n ");
 scanf("%d",&n);
 printf("\n Start entering the words below, :\n");


 switch(1)
 {
  case 1:
   scanf("%s",a1);
   p1=a1;
   printf("\n");
   p1=processing(p1);
   if(n==1)
      break;

   case 2:
   scanf("%s",a1);
   p2=a1;
   printf("\n");
   p2= processing(p2);
   if(n==2)
     break;

  case 3:
   scanf("%s",a1);
   p3=a1;
   printf("\n");
   p3= processing(p3);
   if(n==3)
     break;

  case 4:
   scanf("%s",a1);
   p4=a1;
   printf("\n");
   p4= processing(p4);
   if(n==4)
     break;

  case 5:
   scanf("%s",a1);
   p5=a1;
   printf("\n");
   p5= processing(p5);
   if(n==5)
     break;

  case 6:
   scanf("%s",a1);
   p6=a1;
   printf("\n");
   p6= processing(p6);
   if(n==6)
     break;

   case 7:
   scanf("%s",a1);
   p7=a1;
   printf("\n");
   p7= processing(p7);
   if(n==7)
       break;

  case 8:
   scanf("%s",a1);
   p8=a1;
   printf("\n");
   p8= processing(p8);
   if(n==8)
     break;

  case 9:
   scanf("%s",a1);
   p9=a1;
   printf("\n");
   p9= processing(p9);
   if(n==9)
     break;

  case 10:
   scanf("%s",a1);
   p10=a1;
   printf("\n");
   p10= processing(p10);
   if(n==10)
     break;

  default: printf("Error");
 }

 switch(1)
 {
  case 1: printf("\n %s",p1);
   if(n==1)
     break;

  case 2: printf("\n %s",p2);
   if(n==2)
     break;

  case 3: printf("\n %s",p3);
   if(n==3)
     break;

  case 4: printf("\n %s",p4);
   if(n==4)
     break;

  case 5: printf("\n %s",p5);
   if(n==5)
     break;

  case 6: printf("\n %s",p6);
   if(n==6)
     break;

  case 7: printf("\n %s",p7);
   if(n==7)
     break;

  case 8: printf("\n %s",p8);
   if(n==8)
     break;

  case 9: printf("\n %s",p9);
   if(n==9)
     break;

  case 10: printf("\n %s",p10);
   if(n==10)
     break;

 }
 printf("\n For more intresting programs, visit >> www.EngineersBurger.com \n - Jay Akbari ");
 getch();
}

char* processing(char *a)
{ int i, n,j=0;
 char* temp ;
 n=strlen(a);
       // printf("\n string lenght is %d", n);
 temp= (char*) malloc(n*sizeof(char) + 3);

 for( i=0; *(a+i)!='\0'; i+=2)
 {
  *(temp+j)=*(a+i);
  j++;
  if(*(a+i+1)=='\0')
   break;
 }
 *(temp+j)=' ';
 *(temp+j+1)=' ';
 j++;

 for( i=1; *(a+i)!='\0'; i+=2)
 {
  *(temp+j)=*(a+i);
  j++;
  if(*(a+i+1)=='\0')
   break;
 }
 *(temp+j)= '\0';

 return(temp);
}

Download the .C file : CLICK HERE



0 comments:

Post a Comment

Have A Wonderful Day !

--------------------------------------------------------------------

Word Of The Day | Learn And Use A New Word Everyday!

--------------------------------------------------------------------

"Insidious"

Working or spreading in a hidden and usually injurious way

"glaucoma is an insidious disease"

Copyright © 2016 Engineers Burger - All Rights Reserved. Digital Craftmanship - Nikhil Jha and Jay Akbari.
MyBloggerLab.com