81. Program to accept two strings and compare those two strings
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.
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.
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
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.
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 reverse string”);
}
86. Program to accept two 3 dimensional array and store subtraction of those two arrays into third array.
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
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
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
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
if(line[i]==’\n’) line[i]=’\0’; return i;
}
89.Program to print 4 dimensional matrix with constant number.
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.Program to accept a string and print each word in reverse
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
if(name[i]==’ ‘|| name[i]==’\n’)
{
end=i;
while((end--)>=start)
{
printf(“%c”,name[end]);
}
start=i+1;
}
getch( );
}
91. Program to accept elements into single dimensional array and print the array in ascending order by using three different arrays.
void read_array(int x[]); void sort_array(int y[]); void print_array(int z[]); main()
{
int a[10]; clrscr( ); read_array(a); sort_array(a);
print_array(a);
getch( );
}
void read_array(int x[])
{
int i;
for(i=0;i<10;i++)
{
printf(“enter value for a[%d]”,i);
scanf(“%d”,&x[i]);
}
}
void sort_array(int y[])
{
int i,j,k; for(i=0;i<9;i++) for(j=i+1;j<=9;j++) if(y[i]>y[j])
{ k=y[i]; y[i]= y[j]; y[j]=k;
}
}
void print_array(int z[])
{
int i; for(i=0;i<10;i++) printf(“%d\n”,z[i]);
}
92.Program to accept data and store the given data into file print the data.
main( )
{
FILE *fp; char c; fp=fopen(“data.dat”,”w”); clrscr();
printf(“enter text”);
while(1)
{
c=getchar( ); if(c==eof( )) break; putc(c);
}
fclose(fp); fp=fopen(“data.dat”,”r”); while(1)
{
c=getc(fp);
if(c==eof( ))
break;
putchar(c);
}
getch( );
fclose(fp);
}
93. Program to accept data in lower case and store the given data into file into upper case and print the data.
main( )
{
FILE *fp; Char c; fp=fopen(“data2.dat”,”w”); clrscr( );
printf(“enter text”);
while((c=getchar( ))!=eof( ))
{
putc(toupper(c),fp)
} fclose(fp); fp=fopen(“data2.dat”,”r”); while(1)
{
c=getc(fp); if(c==eof( )) break; putchar(c);
}
getch( );
fclose(fp);
}
94. Program to copy contents of one file into another.
main( )
{
FILE * fp1,*fp2; char ch; fp1=fopen(“text1”,”w”); printf(“enter the text”); while((ch=getchar()!=EOF); putc(ch,fp1);
fclose(fp1); fp1=fopen(“text1”,”r”); fp2=fopen(“text2”,”w”); while((ch=getc(fp1))!=EOF) putc(ch,fp2);
fcolse(fp1); fcolse(fp2); getch( );
}
95. Program to create a file of numbers and copy odd number into second file and even number into third file
main( )
{
FILE *fp1,*fp2,*fp3; int i; fp1=open(“data1”,w”); printf(“enter the number”); scanf(“%d”,&i); while(i!=eof)
{ putw(i,fp1); scanf(“%d”,&i);
}
fcolse(fp1); fp1=fopen(“data1”,”r”); fp2=fopen(“data2”,”w”); fp3=fopen(“data3”,”w”); while((i=getc(fp1))!=eof) if(i%2==0)
putc(i,fp3);
else putw(i,fp2); fcolse(fp1); fcolse(fp2); fcolse(fp3); getch( );
}
96. Program to accept a string in lower case and print first character of each word in upper case.
main( )
{
char str1[80]; int length,i; clrscr( );
printf(“enter a string; “);
length=getline(str1,80);
for(i=0;i
{
str1[0]-=32; if(str1[i]= =’ ‘) str1[i+1]-=32; printf(“%c”.str1[i]);
}
getch();
}
int getline(char line [], int lim)
{
int i;
for(i=0;i
if(line[i]= =’\n’) line[i]=’\0’; return i;
}
97. Program to accept two numbers and interchange two values using functions.
void swap (int a, int b);
main( )
{
int a,b;
clrscr( );
printf(“enter value for a;”); scanf(“%d”,&a); printf(“enter value for b;”); scanf(“%d”,&b);
swap(a,b);
getch( );
}
void swap(int a,int b)
}
int c; c=a; a=b; b=c;
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
}
98. Program for example of static variable.
static int i=1; main( )
{
int j;
clrscr( );
for (j=1;j<=5;j++);
fun( );
getch( );
}
fun( )
{ printf(“\n%d”,i); i=i+1;
}
99.Program to accept a string and print by trailing spaces.
main( )
{
char n,n1;
clrscr ( );
printf(“enter a string;”); while((n=getchar( )!=’\n’) if(n>=’a’ && n<=’z’) putchar(n);
else
if(n>=’a’ && n<=’z’)
putchar(n);
getch( );
}
100. Program to print anti diagonal.
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(“enter which number you want;”);
scanf(“%d”,&c); for(i=0;i<4;i++) for(j=0;j<4;j++) if(i+j= =3) a[i]]j]=c;
else a[i][j]=0 for(i=0;i<4;i++)
{ for(j=0;j<4;j++) printf(“%d”,a[i][j]); printf(“\n”);
}
getch( );
}
No comments:
Post a Comment