E.BALAGURUSAMY SOLUTION PROGRAMMING IN ANSI C: CHAPTER-8


8.3 Write a program to extract a portion of a character string and print the extracted string. Assume that m characters are extracted, starting with the nth character.

Answer:

//Write a program to extract a portion of a character string and print the extracted string.
//Assume that m characters are extracted, starting with the nth character.
//Date: 18/03/2010#include
#include
#define MAX 50
void main()
{
   char Str1[MAX];
   int i,m,n,j;
   clrscr();
   printf("Enter A String:--\n");
   scanf("%[^\n]s",Str1);
   printf("\nEnter Number of Characters Which U Wnat to Extract-->\n");
   scanf("%d",&m);
   printf("\nEnter Beginnig Index from Which U Want to Extract-->\n");
   scanf("%d",&n);
   printf("\nExtracted String is:--\n\n");
   for(i=n-1;i

getch();

}

 

 

8.7 A Maruti car dealer maintains a reecord of sales of various vehicles in the following

      form:

 

               Vehicle Type                          Month of sales                                   Price

               MARUTI-800                                    02/01                                                   210000

               MARUTI-DX                                    07/01                                                   265000

               GYPSY                                  04/02                                                   315750

               MARUTI-VAN                     08/02                                                   240000

 

     Write a program to read this data into a table of strings and output the details of a

     particular vehicle sold during a specified period. The program should request the user to

     input the vehicle type and the period (starting month, ending month). 

 

Answer:

#include<stdio.h>

#include<conio.h>

#define MAX 10

void main()

{

char Veh[MAX][MAX]={“”};

char Vehicle[MAX];

int St_Mon[MAX],En_Mon[MAX],StMon,EnMon;

long int Price[MAX];

int n,i;

clrscr();

printf(“How Entries U Want to Enter\n”);

scanf(“%d”,&n);

printf(“Enter Vehcle Type,Starting Month, Ending Month & Price:–\n”);

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

{

scanf(“%s”,Veh[i]);

scanf(“%d”,&St_Mon[i]);

scanf(“%d”,&En_Mon[i]);

scanf(“%ld”,&Price[i]);

}

clrscr();

printf(“Vehicle Type       Month of Sales         Price\n”);

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

{

printf(“%s            0%d / 0%d             %ld\n”,Veh[i],St_Mon[i],En_Mon[i],Price[i]);

}

printf(“Enter The Type of Vehicle\n”);

scanf(“%s”,Vehicle);

printf(“Enter the Starting & Ending Month\n”);

scanf(“%d %d”,&StMon,&EnMon);

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

{

if((strcmp(Veh[i],Vehicle)==0))

{

if(St_Mon[i]>=StMon)

{

if(En_Mon[i]<=EnMon)

{

printf(“Vehicle Type  %s is Sold During Period 0%d/0%d\n”,Vehicle,StMon,EnMon);

getch();

exit(0);

}

}

}

}

printf(“Vehicle Type  %s Not Sold During Period 0%d/0%d\n”,Vehicle,StMon,EnMon);

getch();

}

 

 

8.9 Write a program that reads the cost of an item in the form RRRR.PP (where RRRR denotes Rupees and PP denotes Paise) and converts the value to a string of words that express the numeric value in words. For example, if we input 125.75 the ouput should be“ONE HUNDRED TWENTY FIVE AND PAISE SEVENTY FIVE”.

 

Answer:

#include<stdio.h>
#include<conio.h>
void main()
{
   float Cost,Pai,Re,j;
   int Rup,i,R;
   clrscr();
   printf("\nEnter Cost of an ITEM-->\n");
   scanf("%f",&Cost);
   Rup = Cost;
   Pai = (Cost - Rup)*100;
   i=Rup/100;
   switch(i)
   {
               case 1: printf("ONE HUNDRED "); break;
               case 2: printf("TWO HUNDRED "); break;
               case 3: printf("THREE HUNDRED "); break;
               case 4: printf("FOUR HUNDRED "); break;
               case 5: printf("FIVE HUNDRED "); break;
               case 6: printf("SIX HUNDRED "); break;
               case 7: printf("SEVEN HUNDRED "); break;
               case 8: printf("EIGHT HUNDRED "); break;
               case 9: printf("NINE HUNDRED "); break;
   }
   i=Rup%100;
   R=i/10;
   Re=(float)i/10;
   switch(R)
   {
               case 1: printf("TEN"); break;
               case 2: printf("TWENTY "); break;
               case 3: printf("THIRTY "); break;
               case 4: printf("FOURTY "); break;
               case 5: printf("FIFTY "); break;
               case 6: printf("SIXTY "); break;
               case 7: printf("SEVENTY "); break;
               case 8: printf("EIGHTY "); break;
               case 9: printf("NINETY "); break;
   }
   R=(Re-R)*10;
   switch(R)
   {
               case 1: printf("ONE"); break;
               case 2: printf("TWO "); break;
               case 3: printf("THREE "); break;
               case 4: printf("FOUR "); break;
               case 5: printf("FIVE "); break;
               case 6: printf("SIX "); break;
               case 7: printf("SEVEN "); break;
               case 8: printf("EIGHT "); break;
               case 9: printf("NINE "); break;
   }
   printf("AND PAISE ");
   i=Pai/10;
   Re=(float)Pai/10;
   R=(Re-i)*10;
   switch(i)
   {
               case 1: printf("TEN"); break;
               case 2: printf("TWENTY "); break;
               case 3: printf("THIRTY "); break;
               case 4: printf("FOURTY "); break;
               case 5: printf("FIFTY "); break;
               case 6: printf("SIXTY "); break;
               case 7: printf("SEVENTY "); break;
               case 8: printf("EIGHTY "); break;
               case 9: printf("NINETY "); break;
   }
   switch(R)
   {
               case 1: printf("ONE"); break;
               case 2: printf("TWO "); break;
               case 3: printf("THREE "); break;
               case 4: printf("FOUR "); break;
               case 5: printf("FIVE "); break;
               case 6: printf("SIX "); break;
               case 7: printf("SEVEN "); break;
               case 8: printf("EIGHT "); break;
               case 9: printf("NINE "); break;
   }
   getch();
}




8.10 Develop a program that will read and store the details of a list of students in the format

 

   Roll No.                                  Name                          Marks Obtained

………….                                  ………                        ………………..

………….                                  ………                        ………………..

 

And produce the following output lists:

a)      Alphabetical list of names, roll numbers and marks obtained.

b)     List sorted on roll numbers.

c)      List sorted on marks (rank-wise list)

 

 

Answer:

 

#include<stdio.h>

#include<conio.h>

#define MAX 50

void main()

{

char Stu_Name[MAX][MAX]={“”};

//char Stu_Name1[MAX][MAX]={“”};

char Temp[MAX]=””;

int roll_No[MAX],Marks[MAX],n,i,In[MAX],Roll_No1[MAX],Marks1[MAX];

int Temp1,Temp2;

int j;

clrscr();

printf(“How Many Student Name U Want to Enter\n\n”);

scanf(“%d”,&n);

printf(“Enter Roll No. & Students Name & Total Marks:–\n”);

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

{

scanf(“%d”,&Roll_No[i]);

scanf(“%s”,Stu_Name[i]);

scanf(“%d”,&Marks[i]);

}

clrscr();

printf(“Roll No      Name          Marks\n”);

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

{

printf(“%d%s%d\n”,Roll_No[i],Stu_Name[i],Marks[i]);

}

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

{

for(j=0;j<n-i-1;j++)

{

if(strcmp(Stu_Name[j],Stu_Name[j+1])>0)

{

strcpy(Temp,Stu_Name[j]);

strcpy(Stu_Name[j],Stu_Name[j+1]);

strcpy(Stu_Name[j+1],Temp);

Temp1=Roll_No[j];

Roll_No[j]=Roll_No[j+1];

Roll_No[j+1]=Temp1;

Temp2=Marks[j];

Marks[j]=Marks[j+1];

Marks[j+1]=Temp2;

}

}

}

printf(“\nAccording to Student Names:–\n”);

printf(“Roll No      Name          Marks\n”);

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

{

printf(“%d            %s            %d\n”,Roll_No[i],Stu_Name[i],Marks[i]);

}

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

{

for(j=0;j<n-i-1;j++)

{

if(Roll_No[j]>Roll_No[j+1])

{

strcpy(Temp,Stu_Name[j]);

strcpy(Stu_Name[j],Stu_Name[j+1]);

strcpy(Stu_Name[j+1],Temp);

Temp1=Roll_No[j];

Roll_No[j]=Roll_No[j+1];

Roll_No[j+1]=Temp1;

Temp2=Marks[j];

Marks[j]=Marks[j+1];

Marks[j+1]=Temp2;

}

}

}

printf(“\nAccording to Marks:–\n”);

printf(“Roll No      Name          Marks\n”);

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

{

printf(“%d            %s            %d\n”,Roll_No[i],Stu_Name[i],Marks[i]);

}

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

{

for(j=0;j<n-i-1;j++)

{

if(Marks[j]<Marks[j+1])

{

strcpy(Temp,Stu_Name[j]);

strcpy(Stu_Name[j],Stu_Name[j+1]);

strcpy(Stu_Name[j+1],Temp);

Temp1=Roll_No[j];

Roll_No[j]=Roll_No[j+1];

Roll_No[j+1]=Temp1;

Temp2=Marks[j];

Marks[j]=Marks[j+1];

Marks[j+1]=Temp2;

}

}

}

printf(“\nAccording to Roll No:–\n”);

printf(“Roll No      Name          Marks\n”);

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

{

printf(“%d            %s            %d\n”,Roll_No[i],Stu_Name[i],Marks[i]);

}

getch();

}

 

 

8.11 Write a program to read to strings and compare them using the function strcmp() and print a mesaage that the first string is equal, less or greater than the second one.

 

Answer:

#include<stdio.h>

#include<conio.h>

#define MAX 50

void main()

{

char Str1[MAX],Str2[MAX];

 

clrscr();

 

printf(“Enter First String:–\n”);

scanf(“%[^\n]s”,Str1);

 

fflush(stdin);

 

printf(“Enter Second String:–\n”);

scanf(“%[^\n]s”,Str2);

 

if(strcmp(Str1,Str2)==0)

printf(“\nBoth Strings are Equal\n”);

else if(strcmp(Str1,Str2)<0)

printf(“\nFirst String is Less Than\n”);

else

printf(“\nFirst String is Greater Than\n”);

getch();

}

 

 

8.12 Write a program to read a line of text from the keyboard and print out the number of occurrences of a given substring using the function strstr().

Answer:

#include<stdio.h>

#include<conio.h>

 

#define MAX 50

 

void main()

{

char *Str1,*Str2,*Str3;

int i,Len,Len1,Count;

 

clrscr();

 

Count=0;

 

printf(“Enter Text:–\n”);

scanf(“%[^\n]s”,Str1);

 

fflush(stdin);

 

printf(“Enter Substring:–\n”);

scanf(“%[^\n]s”,Str2);

 

Len=strlen(Str1);

Len1=strlen(Str2);

 

strcpy(Str3,Str1);

 

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

{

if((Str3=strstr(Str3,Str2))!=NULL)

{

i=i+Len1;

Count=Count+1;

}

strcpy(Str3,(Str3+Len1));

}

 

printf(“\n\nNumber of occurences is:– %d”,Count);

getch();

}

 

 

8.13 Write a program that will copy m consecutive characters from a string s1 beginning at position n into another string s2.

 

Answer:

#include<stdio.h>

#include<conio.h>

 

#define MAX 50

 

void main()

{

char Str1[MAX],Str2[MAX];

int i,m,n,j;

 

clrscr();

 

printf(“Enter A String:–\n”);

scanf(“%[^\n]s”,Str1);

 

printf(“\nEnter Number of Characters Which U Wnat to Copy–>\n”);

scanf(“%d”,&m);

 

printf(“\nEnter Beginnig Index from Which U Want to Copy–>\n”);

scanf(“%d”,&n);

 

for(i=n-1,j=0;i<m+n;i++,j++)

{

Str2[j]=Str1[i];

}

 

Str2[m]=”;

 

printf(“\n\nCopied String is–> %s \n\n”,Str2);

getch();

}

 

 

8.14 Write a program to create a directory of students with roll numbers.The program should display the roll number for a specified name and vice-versa.

 

Answer:

#include<stdio.h>

#include<conio.h>

 

#define MAX 50

void main()

{

char Stu_Name[MAX][MAX],Name[MAX];

int Roll_No[MAX],n,i,Roll,Index;

 

clrscr();

 

printf(“How Many Student Name U Want to Enter\n\n”);

scanf(“%d”,&n);

 

printf(“Enter Roll No. & Students Name:–\n”);

 

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

{

scanf(“%d”,&Roll_No[i]);

scanf(“%s”,Stu_Name[i]);

}

 

printf(“\nEnter Student Roll No which U want to Search:–\n”);

scanf(“%d”,&Roll);

 

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

{

if(Roll==Roll_No[i])

{

Index=i;

}

}

 

printf(“\nName of Student is –> %s whose Roll No is:–%d”,Stu_Name[Index],Roll);

 

printf(“\n\nEnter Student Name which U want to Search:–\n”);

scanf(“%s”,Name);

 

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

{

if(strcmp(Stu_Name[i],Name)==0)

{

Index=i;

}

}

 

printf(“\n\nRoll No of is:– %d Student Whose Name is:– %s\n”,Roll_No[Index],Stu_Name[Index]);

getch();

}

 

 

 

8.15 Given a string char str[ ] =”123456789”; Write a program that displays the following:

            1

         2 3 2

      3 4 5 4 3

   4 5 6 7 6 5 4

5 6 7 8 9 8 7 6 5

 

 

answer:

#include<stdio.h>

#include<conio.h>

 

void main()

{

char Str[]=”123456789″;

int i,j,k,l;

clrscr();

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

{

for(k=4;k>i;k–)

{

printf(” “);

}

for(j=0,l=i;j<=i;j++,l++)

{

printf(“%c”,Str[l]);

}

l=l-2;

for(k=0;k<i;k++,l–)

printf(“%c”,Str[l]);

printf(“\n”);

}

getch();

 

}

 

 

 

 

Advertisements

3 thoughts on “E.BALAGURUSAMY SOLUTION PROGRAMMING IN ANSI C: CHAPTER-8

  1. I came to your “E.BALAGURUSAMY SOLUTION PROGRAMMING IN ANSI C: CHAPTER-8 | assignment2do” page and noticed you could have a lot more traffic. I have found that the key to running a website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get traffic from and they let you try it for free. I managed to get over 300 targetted visitors to day to my website. Visit them today: http://bag.sh/16M

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s