Page 9
81. Program to accept two strings and compare those two strings
#include<stdio.h>
#include<conio.h>
int strcomp (char *pt1, char *pt2);
void read-string(char*pt);
main( )
{
char line [80],line2[80];
clrscr( );
printf(“enter first string;”);
read-string (line1);
printf(“enter second string”);
read-string(line2);
if(strcomp (line1,line2)>0)
printf(“second string biggest”);
else
if(strcomp (line1,line2)>0)
printf(“ first string biggest;”);
else
printf(“both the strins are equal”);
getch( );
}
void read-string(char*pt)
{
for(;(*pt=getchar( ))!=’\n’;pt++);
*pt=’\0’;
}
int strcomp (char *pt1, char *pt2)
{
for(;*pt1!=’\0’;pt1++;pt2++)
if(*pt1!=*pt2)
break;
return *pt1-*pt2;
}
82. Program to accept a string using pointers and functions.
#include<stdio.h>
#include<conio.h>
main( )
{
int ch[20];
clrscr ( );
printf(“enter a string”);
read_array(ch);
printf(“%s”,ch);
getch( );
}
void read_string (char*pt)
{
for(;(*pt=getchar( ))!=’/n’;pt++);
*pt=’\0’;
}
83.Program to read a string and print the first two characters of each word in the string.
#include<stdio.h>
#include<conio.h>
main( )
{
char s[100];
int i,l;
clrscr( );
printf(“enter a string”);
gets(s);l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]!=’ ‘ && s[i]=’ ‘)
{
printf(“%c %c”,s[i],s[i+1])
i=i+2;
while(s[i]!=’ ‘)
i++;
}
}
getch( );
}
84.Program to accept two numbers and print the sum of given two numbers by using pointers
#include<stdio.h>
#include<conio.h>
main( )
{
int a, b,c;
clrscr( );
a=10;
b=20;
c=*(&a)+*(&b);
printf(“%d”,c);
getch( );
}
85.Program to accept a string and print reverse of the given string by using functions.
#include<stdio.h>
#include<stdio.h>
int getline (char str[]);
void printline (char str[],int i);
main( )
{
char str[80];
int 1;
clrscr( );
1=getline(str );
printline(str,1);
printline(str,1);
getch ( );
}
int getline(char str[])
{
int 1;
printf(“enter a string;”);
for(i=0;i<80&&((str[i]=getchar())!=’\n’);i++);
if(str[i]=’\0’;
return i;
}
void printline(char str[],int 1)
{
int j;
for(j=1;j<=0;j--)
printf(“%c”,str[j]);
printf(‘is the revefrse string”);
}
86. Program to accept two 3 dimensional array and store subtraction of those two arrays into third array..
#include<stdio.h>
#include<conio.h>
main( )
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr( );
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf(“enter two values for a[%d][%d] & b[%d][%d]:”,i,j,i,j);
scanf(“%d%d”,&a[i][j],&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf(“%d”,,c[i][j]);
}
printf(“\n”);
}
getch( );
87.Program to accept a single dimensional array and print them by using pointers
#include<stdio.h>
#include<conio.h>
main( )
{
int a[5],*b,i;
clrscr( );
b=&a[0];
for(i=0;i<=4;i++)
{
printf(“enter the a value for a[%d]”,i)
scanf(“%d”,b);
b++;
}
b=&a[0];
for(i=0;i<=4;i++)
{
printf(“\n%d”,*b);
b++;
}
getch( );
}
88.Program to accept two strings and biggest among them
#include<stdio.h>
#include<conio.h>
int getline(char line[],int lim);
main( )
{
char str1[80],str2[80];
int len1,len2;
clrscr( );
printf(“enter first string”);
len1=getline(str1,80);
printf(“enter second string”);
len2=getline(str1,80);
if(len1 >len2)
printf(“first string bigger than second string”);
else
if(len1<len2)
printf(“second string bigger than first string”);
else
printf(“both strings are equal”);
getch( );
}
int getline(char line[],int lim)
{
int i;
for(Ii0;i<lim && ((line[i]=getchar( ))!=’\n’);i++)
if(line[i]==’\n’)
line[i]=’\0’;
return i;
}
89.Program to print 4 dimentional matrix with constant number.
#include<stdio.h>
#include<conio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(“enter constant number”);
scanf(“%d”,&c);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
a[i][j]=c;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(“%d”,a[i][j]);
printf(“\n”);
}
getch( );
}
90.Prongram to accept a string and print each word in reverse
#include<conio.h>
#include<stdio.h>
main( )
{
char name[80];
int i,j,start=0,end,len;
clrscr( );
printf(“enter a string”);
scanf(“%s”,name);
for(i=0;i<80 &&((name[i]=getchar( ) )!=’\n’);i++);
len=i;
for(i=0;i<len;i++)
if(name[i]==’ ‘|| name[i]==’\n’)
{
end=i;
while((end--)>=start)
{
printf(“%c”,name[end]);
}
start=i+1;
}
getch( );
0 comments