Control Flow: C programming solved programs/examples with solutions

131

Control Flow: C programming solved programs/examples with solutions | c programming questions | basic c programs | c programs for interview | pattern programs in c

  • Write a function htoi(s), which converts a string of hexa-decimal digits (including an optional 0x  or 0X) into its equivalent integer value.

(The allowed digits are 0 through 9, a through f, and A through F)

Input: –  oxAA

Output: –  170

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define EXIT_SUCCESS 0

long htoi(char *);

void strrev(char *);

int main()

{

   long number=0;

   char inputstr[30];

   printf(“Enter a hexadecimal string “);

   scanf(“%s”,inputstr);

   printf(“string is %s\n”,inputstr);

   number=htoi(inputstr);

   printf(“Integer is %ld\n”,number);

   return EXIT_SUCCESS;

}

long htoi(char * inputstr)//converts the hexrepresentation to integer

{

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

   long hnumber=0;

   char str[30];

   strrev(inputstr);

   length=strlen(inputstr);

//to include optional ox

   if((inputstr[length-1]==’O’ || inputstr[length-1]==’o’)&&(inputstr[length-2]==’X’ || inputstr[length-2]==’x’))

   {

        length=length-2;

   }

   while(i<=length-1)

   {if(inputstr[i]==’a’||inputstr[i]==’A’)

          hnumber=hnumber+base*10;

        else if(inputstr[i]==’b’||inputstr[i]==’B’)

          hnumber=hnumber+base*11;

        else if(inputstr[i]==’c’||inputstr[i]==’C’)

          hnumber=hnumber+base*12;

        else if(inputstr[i]==’d’||inputstr[i]==’D’)

          hnumber=hnumber+base*13;

   else if(inputstr[i]==’e’||inputstr[i]==’E’)

          hnumber=hnumber+base*14;

        else if(inputstr[i]==’f’||inputstr[i]==’F’)

          hnumber=hnumber+base*15;

        else

        {

           str[0]=inputstr[i];

           str[1]=’\0′;

           hnumber=hnumber+(base*atoi(str));

        }

        base=base*16;

        i++;

   }

   printf(“%ld\n”,hnumber);

   return hnumber;

}

void strrev(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–;

   }

}

Output:

[[email protected] ~]$ gcc hextoint.c -o htoi

[[email protected] ~]$ ./htoi

Enter a hexadecimal string oxAA

Integer is 170


  • Write a function squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.

Input: –            

S1 = “character”

S2 = “at”

Output: –

S1=”chrcer”

S2

:q=”at”

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

void squeeze(char*,char*);

int main()

{

 char s1[15],s2[15]=””;

 printf(“Enter first string:”);

 scanf(“%s”,s1);

 printf(“Enter the second string:”);

 scanf(“%s”,s2);

 squeeze(s1,s2);

 printf(“Final result after squeezing is %s\n”,s1);

 return 0;

}

void squeeze(char *s1,char *s2)

{

 int i,j,k=0;

 for(i=0;i<strlen(s1);i++)

  for(j=0;j<strlen(s2);j++)

  {

   if(s1[i]==s2[j])

{

    for(k=i;k<(strlen(s1))-1;k++)

    {

      s1[k]=s1[k+1];

    }

     s1[k]=’\0′;

   }

  }

}

Output:

[[email protected] ~]$ gcc squeeze.c

[[email protected] ~]$ ./a.out

Enter first string:character

Enter the second string:at

Final result after squeezing is chrcer


  • Write a function any(s1,s2) which returns

the first location in the string s1 where any character from the string s2 occurs, or

-1 if s1 contains no characters from s2.

i) Input: –         S1 = “character”    S2 = “abc” 

   Output: –      Function should return “3”

ii)Input: –         S1 = “character”    S2 = “xyz” 

   Output: –      Function should return “-1”

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int any(char *,char *);

int main()

{

  char s1[15],s2[15];

  int pos;

   printf(“Enter first string:”);

   scanf(“%s”,s1);

   printf(“Enter the second string:”);

   scanf(“%s”,s2);

   pos=any(s1,s2);

   printf(“position at which it occurs is %d\n”,pos);

   return 0;

}

  int any(char *s1,char *s2)

  {

    int i,j=0;

    int pos=-1;

     for(i=0;i<(strlen(s2));i++)

      for(j=0;j<(strlen(s1));j++)

        if(s1[i]==s2[j])

{

         pos=i+1;

          break;        }

        return pos;

      }

Output:

[[email protected] ~]$ gcc any.c -o any

[[email protected] ~]$ ./any

Enter first string:character

Enter the second string:abc

pos at which it occurs is 3

[[email protected] ~]$ ./any

Enter first string:character

Enter the second string:xyz

pos at which it occurs is -1


  • Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted(i.e., 1 changed to 0 and vice versa), leaving the others unchanged.

Input: –  Invert(45,3,2)

Output:- 53

  • Write a function rightrot(x,n) that returns the value of the integer x rotated to the right by n bit positions.

Input: –  rightrot(48,3)

Output: – x=9

  • In a two’s complement number system, x &=  (x-1) deletes the rightmost 1-bit in x. Explain.

Input: – X= 45

Output: –  X=44

  • .

(Use conditional expression instead of  if-else)

Input: – S1=”APPLE”

Output:= S1=”apple”

#include<stdio.h>

#include<string.h>

int lowerfunc(int);

int main()

{

 char bound[30];

 char *p;

  printf(“enter the string “);

  scanf(“%s”,bound);

  p=bound;

 int result=0;

 while(‘\0’!=*p)/*while the string is not null*/

 {

  result=lowerfunc(*p);

  printf(“%c”,result);

  ++p;

 }

return 0;

}

int lowerfunc(int val)

{

{

 if(val>=65 && val<=90)/*ASCII value of A-65,Z-90,if the input is in between these ascii values*/

  return val+97-65;/*difference will be the same value to get lower case letter*/

 else

 return val;

}

Output:

[[email protected] ~]$ gcc tolow_func.c -o tolow

[[email protected] ~]$ ./tolow

enter the string APPLE

apple   

  • Write a function escape(s,t) that converts characters like newline and tab into visible escape                      sequences like \n and \t as it copies the string t to s. Use a switch.

Input : – S1=” Encapsulation

Inheritance       polymorphism”

  • Output: – S1=”Encapsulation\nInheritance\tpolymorphism”;

#include<stdio.h>

#define exit 0

void escape(char *s,char *t);

int main()

{

 char s1[100]=”Encapsulation \n Inheritance \t \t Polymorphism”;

 char s2[100];

 printf(“entered string is:%s :\n”,s1);

 escape(s2,s1);

 printf(“Escaped string is:\n %s \n”,s2);

 return exit;

}

  void escape(char *s,char *t)

  {

    int i=0,j=0;

  while(t[i])

     {

      switch(t[i])

      {

        case ‘\n’:

             s[j++]=’\\’;

             s[j]=’n’;

             break;

        case ‘\t’:

             s[j++]=’\\’;

             s[j]=’t’;

             break;

        default:

             s[j]=t[i];

             break;

   }

   ++i;

   ++j;

  }

   s[j]=t[i];

}

Output:

[[email protected] ~]$ gcc escape.c

[[email protected] ~]$ ./a.out

entered string is:Encapsulation

 Inheritance             Polymorphism :

Escaped string is:

 Encapsulation \n Inheritance \t \t Polymorphism


  • Write a function which converts escape sequences into the real characters.
    1. Input: – S1=”Encapsulation\nInheritance\tpolymorphism”;
    1. output : – S1=” Encapsulation
    1. Inheritance       polymorphism”

#include<stdio.h>

#define exit 0

void escapetoreal(char *s,char *t);

int main()

{

 char s1[100]=”\nEncapsulation\nInheritance\t\tPolymorphism”;

 char s2[100]=””;

 printf(“Entered string is %s\n:”,s1);

 escapetoreal(s1,s2);

 printf(“after converting the escape sequences to real:%s”,s1);

 return exit;

}

void escapetoreal(char *s,char *t)

{

 int i=0,j=0;

 while(t[i])

{

  switch(t[i])

  {

  case ‘\\’:

      switch(t[++i])

      {

        case ‘n’:

               s[j]=’\n’;

               break;

        case ‘t’:

               s[j]=’\t’;

               break;

        /*

        case ‘\”‘:

                s[j]=’\”‘;

                break;  */

        default:

               s[j++]=’\\’;

               s[j]=t[i];

      }

      break;

    default:

           s[j]=t[i];

}

   i++;

   j++;

 }

}

Output:

[[email protected] ~]$ gcc Escape_to_real.c -o escape

[[email protected] ~]$ ./escape

Entered string is

Encapsulation

Inheritance             Polymorphism

:after converting the escape sequences to real:

Encapsulation

Inheritance             Polymorphism

Leave a Reply

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