Exercise [D]
(a)
Write down two functions xgets( ) and xputs( ) which work similar to the standard library functions gets( ) and puts( ).
Solution:
#include<stdio.h>
#include<conio.h>
void xgets();
void xputs();
void main() {
char str[80];
clrscr();
xgets(str);
printf("\n");
xputs(str);
getch();
}
void xgets( char *s) {
int i=0;
char ch;
for(i=0;i<=79;i++) {
ch=getche();
if(ch=='\r') {
*s='\0';
break;
}
if(ch=='\b') {
printf("\b");
i-=2;
s-=2;
}
else {
*s=ch;
s++;
}
}
}
void xputs( char *s) {
while(*s!='\0') {
putch(*s);
s++;
}
}
---------------------------------------------------------------------------------------------------------------
(b) Write down a function getint( ), which would receive a numeric string from the keyboard, convert it to an integer number and return the integer to the calling function. A sample usage of getint( ) is shown below:
main( )
{
int a ;
a = getint( ) ;
printf ( "you entered %d", a )
}
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int a;
char s[80];
printf("Enter any numeric string: ");
gets(s);
a=getint(s); /* converting and receiving string as integer */
printf("\n\nYou entered = %d\n",a);
getch();
}
int getint(char s1[80]) {
int digit;
digit=atoi(s1); /* converting string to integer */
return digit;
}
______________________________________________________________________
thankyou bro
ReplyDelete