Function: C programming solved programs/examples with solutions

158

Here we are sharing Functions and Program structure related C programs | c programming questions | basic c programs | c programs for interview | pattern programs in c


  • Write the function itob(n,s,b) that converts the integer n into a base b character representation in the string s.(In particular, itob(n,s,16) formats n as a hexadecimal integer in s).

Input :-  itob(15,s,16)

Output:-  s= “E”

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define EXIT 0

void itob(int, char * ,int);

char * equi(int, char *);

void reverse(char *);

int main()

{

    int number=0, base=0;

    char result[50];

    printf(“Enter integer number: “);

    scanf(“%d”,&number);

    printf(“Enter base “);

    scanf(“%d”,&base);

itob(number,result,base);

    return EXIT;

}

void itob(int number, char * result,int base)

{

    long remainder=0,num=0, snum=number, b=base;

    char c[5];

    while(number>0)

    {

        if(b<10)

        {

            remainder=number%b;

            number=number/b;

            base=base*b;

            strcat(result,equi(remainder,c));

        }

        else

        {

            remainder=number%b;

            if(remainder>9)

            {

                if(remainder==10)   strcat(result,”A”);

                else if(remainder==11)   strcat(result,”B”);

   else if(remainder==12)   strcat(result,”C”);

                else if(remainder==13)   strcat(result,”D”);

                else if(remainder==14)   strcat(result,”E”);

                else if(remainder==15)   strcat(result,”F”);

                else {}

            }

            else

            {

                strcat(result,equi(remainder,c));

            }

            number=number/b;

            base=base*b;

        }

    }

    reverse(result);

    printf(“Input number is:%d \n”,snum);

    printf(“Equivalent of %d in %d base is %s\n”,snum,b,result);

}

char * equi(int number, char * c)

{

    c[0]=(‘1’-1)+number;

  c[1]=’\0′;

    return c;

}

void reverse(char *string)

{

    int length, c;

    char *begin, *end, temp;

    length = strlen(string);

    begin = string;

    end = string;

    for ( c = 0 ; c < ( length – 1 ) ; c++ )

        end++;

    for ( c = 0 ; c < length/2 ; c++ )

    {

        temp = *end;

        *end = *begin;

*begin = temp;

        begin++;

        end–;

    }

}


  • Write a function itoa that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough.

Input: – itoa(40,buffer,5)

Output :-   “   40” (i.e 3 spaces before 40)

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define EXIT_SUCCESS 0

void itoa(int, char *, int);

char * chareq(int, char *);

void reverse(char *);

int main()

{

   int numtoconvert=0, width=0;

   char result[50]=””;

   printf(“Enter integer number to convert to string: “);

   scanf(“%d”,&numtoconvert);

   printf(“Enter width “);

   scanf(“%d”,&width);

   itoa(numtoconvert,result,width);

   return EXIT_SUCCESS;

}

char * chareq(int number, char * c)

{

           switch(number)

  {

                case 0: c[0]=’0′;break;

                case 1: c[0]=’1′;break;

                case 2: c[0]=’2′;break;

                case 3: c[0]=’3′;break;

                case 4: c[0]=’4′;break;

                case 5: c[0]=’5′;break;

                case 6: c[0]=’6′;break;

                case 7: c[0]=’7′;break;

                case 8: c[0]=’8′;break;

                case 9: c[0]=’9′;break;

     }

        c[1]=’\0′;

        return c;

}

void reverse(char *string)

{

   int length, c;

   char *begin, *end, temp;

   length = strlen(string);

begin = string;

   end = string;

   for ( c = 0 ; c < ( length – 1 ) ; c++ )

      end++;

   for ( c = 0 ; c < length/2 ; c++ )

   {

      temp = *end;

      *end = *begin;

      *begin = temp;

      begin++;

      end–;

   }

}

void itoa(int number, char *result, int width)

{

   int rem=0, length=0,i=0,base=1;

   char c[5];

   while(number>0)

  {

     rem=number%10;

     number=number/10;

     base=base*10;

     strcat(result,chareq(rem,c));

   }

   length=strlen(result);

   for(i=length;i<width;i++)

           result[i]=’ ‘;

   result[width]=’\0′;

   reverse(result);

   printf(“The converted string is \’%s\’\n”,result);

}


  • Write a function that returns the right most position of char t in the given string S or   -1 if there is none.

Eg: string S1 = “Testing”

      int Pos = myPos(S1, ‘t’) then Pos =4.

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define EXIT_SUCCESS 0

int rightmostpos(char *, char);

int main()

{

 int position;

 char string[50];char c;

 printf(“Enter character to get its right most occurance  “);

 scanf(“%c”,&c);

 printf(“Enter a string  “);

 scanf(“%s”,string);

 position=rightmostpos(string,c);

 printf(“\nPosition of rightmost occurance of %c in string \’%s\’ is %d\n”,c,string,position);

 return EXIT_SUCCESS;

}

int rightmostpos(char *string, char  c)

{

  int i=0, j=0, k=0, length=0, position=-1;

  length=strlen(string);

  for(i=0;i<length;i++)

if(string[i]==c)

     position=i+1;

  return position;

}

  • Write a function to get the floating point number from the given input.

Eg: Buffer = “123.54”

      myFloat = myfloat(Buffer) then myFloat = 123.54

      if Buffer = “123.453e-6”

     then myFloat = myfloat(Buffer) = 0.00012345

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<math.h>

#define EXIT_SUCCESS 0

void myfloat(char *,float *);

float number;

int pr;

int main()

{

   float *f;

   number=0;int p=0;

   char inputstr[50]=””;

   printf(“Enter input float in string: “);

   scanf(“%s”,inputstr);

   myfloat(inputstr,f);

  // number=*f;p=*(f++);

   printf(“the float number is %.*f\n”,pr,number);

  return EXIT_SUCCESS;

}

void myfloat(char *result,float *f)

{

   int length=0,i=0,intlen=0,decilen=0,exp=0,d=0,il=0;

   float base=1,k;

   length=strlen(result);

   for(i=0;i<length;i++)

   {

        if(result[i]==’.’)

           break;

        intlen++;

   }

   il=intlen;

   while((result[i]!=’e’ || result[i]!=’E’) && i<length)

   {

        i++;

        decilen++;

        if(result[i]==’e’ || result[i]==’E’) break;

   }

   decilen–;

  d=decilen;

   if(result[i]==’e’ || result[i]==’E’)

   {

        i++;

        if(result[i]==’+’) exp=(result[i+1]-(‘1’-1));

        if(result[i]==’-‘) exp=(result[i+1]-(‘1’-1));

   }

   printf(“intlen is %d, decilen is %d, exp is %d\n”,intlen, decilen, exp);

   while(decilen!=0)

   {

      k=result[decilen+intlen]-(‘1’-1);

      number=number+k*base;

      base=base*10;

      decilen–;

   }

   while(intlen!=0)

   {

      k=result[intlen-1]-(‘1’-1);

      number=number+k*base;

      base=base*10;

      intlen–;

   }

   i=d+exp;

number=number/pow(10,i);

   pr=d+exp;

}


  • Write a program which implements getch() and ungetch().

Getch – gets a character from keyboard

Ungetch – push a character onto the input queue


  • Write functions to print the first element of the stack (without popping), to duplicate it and swap the first two elements of the stack.

Eg: Stack = { 4,6,9,12}

      printFirst(Stack) should print 4 and now

      Stack = {6,4,9,12}

#include<stdio.h>

#define max 5

int top=-1,stack[max];

void push();

void display();

void display2();

void displayall();

void swap();

main()

{

 int ch,x;

 do

 {

 printf(“\n 1.Push an element using a stack”);

 printf(“\n 2.Display the first element of the stack”);

 printf(“\n 3.Display the first&second element of the stack”);

 printf(“\n 4.Dispaly all the elements in stack”);

 printf(“\n 5.After swaping the first two elements”);

 printf(“\n 6.Exit”);

 printf(“\nselect your choice:”);

 scanf(“%d”,&ch);

 switch(ch)

{

  case 1:

       printf(“enter an element to push:”);

       scanf(“%d”,&x);

       push(x);

       break;

  case 2:

       display();

       break;

  case 3:

       display2();

       break;

  case 4:

       displayall();

       break;

  case 5:

       swap();

       break;

  default:printf(“Exit”);

          return 0;

 }

 }while(ch!=6);

}

void push(int x)

{

 if(top==max-1)

 {

  display();

  printf(“stack overflow”);

 }

 else

 {

  stack[++top]=x;

  displayall();

 }

 }

void display()

{

  if(top==-1)

 printf(“empty stack”);

/*for(i=top;i>=0;i–)*/

printf(“%4d\n”,stack[0]);

}

void display2()

{

 int i=0;

if(top==-1)

 printf(“empty stack”);

 for(i=0;i<2;i++)

 printf(“%4d\n”,stack[i]);

}

void swap()

{

  int temp[0],j;

  if(top==-1)

 printf(“empty stack”);

// int temp[0]=0,j=0;

 temp[0]=stack[0];

 stack[0]=stack[1];

 stack[1]=temp[0];

 for(j=0;j<=top;j++)

 printf(“%4d\n”,stack[j]);

}

void displayall()

{

int i=0;

  if(top==-1)

 printf(“empty stack”);

for(i=0;i<=top;i++)

printf(“%4d\n”,stack[i]);

}


  • Write a function to clear the stack. (program to delete the elements.)

#include<stdio.h>

#define max 6

int top=-1,stack[max];

void push();

void clear();

void display();

main()

{

  int ch,x;

  do

  {

   printf(“\n\n1.Push a element using a stack\n”);

    printf(“\n2.clear all  elements using stack\n”);

    printf(“\n3.Display all the elements\n”);

    printf(“\n4.Exit Program\n”);

    printf(“\nSelect any one of the above==>”);

    scanf(“%d”,&ch);

    switch(ch)

    {

      case 1: printf(“\nEnter the element to be pushed into the stack==>”);

       scanf(“%d”,&x);

       push(x);

  break;

      case 2: clear();

              break;

      case 3: display();

       break;

      default:printf(“\n EXIT”);

    }

  }while(ch!=4);

}

void push(int x)

{

  if(top==max-1)

  {

 display();

 printf(“Stack overflow….\n”);

  }

  else

  {

 stack[++top]=x;

 display();

  }

  if(top==max-1)

  {

display();

 printf(“The stack is full….\n”);

  }

}

void clear()

{

  if(top==-1)

 printf(“\nStack underflow….\n”);

  else

  {

 int i=0;

 for(i=top;i<=max;i++)

 stack[top–];

 if(top<=0)

        printf(“\nThe stack is empty….\n”);

  }

}

void display()

{

  int i;

  if(top==-1)

printf(“\nEmpty stack….\n”);

  for(i=top;i>=0;i–)

 printf(“\n%4d”,stack[i]);

}


  • Write a function to get a string from the number given.

Eg:  input: str1 = myItoa(1234)

       Output:  str1= “1234”

#include<stdio.h>

#include<string.h>

#define EXIT 0

#define MAX 50

void myitoa(int,char *);

void reverse(char *);

char * chareq(int, char *);

int main()

{

   int number;

   char str[MAX]=””;

   printf(“Enter an integer to be converted into string:”);

   scanf(“%d”,&number);

   myitoa(number,str);

   printf(“After conversion the string is %s\n”,str);

   return EXIT;

}

void myitoa(int number, char * str)

{

   int i=0,j=0,rem=0;char c[2];

while(number!=0)

   {

        rem=number%10;

        strcat(str,chareq(rem,c));

        number=number/10;

        i++;

   }

   reverse(str);

}

void reverse(char *string)

{

   int length, c;

   char *begin, *end, temp;

   length = strlen(string);

   begin = string;

 end = string;

   for ( c = 0 ; c < ( length – 1 ) ; c++ )

      end++;

   for ( c = 0 ; c < length/2 ; c++ )

{

      temp = *end;

      *end = *begin;

      *begin = temp;

      begin++;

      end–;

   }

}

char * chareq(int number, char * c)

{

        c[0]=48+number;

        c[1]=’\0′;

        return c;

}


  •   Write the recursive version for the above function
  1. Write a recursive function reverse (char*  s).

Char* s = “Testing”

Output: s = “gnitseT”

#include<stdio.h>

#define EXIT 0

#define max 50

void reverse(char *);

int main()

{

 char a[max];

 printf(“Enter the string to be reversed:”);

 scanf(“%s”,a);

 reverse(a);

}

 void reverse(char * str)

 {

  if(*str)

  {

   reverse(str+1);

   printf(“%c”,*str);

  }

 }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.