Tomorrow is my persistent aptitude for internship program, and while preparing i found some of the programs which took me little time to cover up. but somehow i did them . i am posting them here, so people can use them in future.. :)
1. program to copy one file content to another, by removing extra spaces.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
void main() {
FILE *fp1, *fp2;
int i=0;
char ch,ch1;
clrscr();
fp1 = fopen("Sample.txt", "r");
fp2 = fopen("Output.txt", "w");
while (1)
{
ch = fgetc(fp1);
if (ch == EOF)
break;
else if(ch==' ')
{
if(i==1)
{
}
else
{
fputc(' ',fp2);
i=1;
}
}
else
{
fputc(ch, fp2);
i=0;
}
}
printf("File copied Successfully!");
fclose(fp1);
fclose(fp2);
getch();
}
*if u are using turbo c, please do this program by keeping into bin folder, I dont know why but it idnt worked properly on other folers.
2. Program to give output as aabbccee if the input is given as abceacbe.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
main()
{
char str[10];
int i=0,j=0;
int count[127];
while(j<=127)
{
count[j]=0;
j++;
}
clrscr();
cout<<"Enter a string\n";
cin>>str;
cout<<str<<"\n";
char c;
while (str[i]!='\0')
{
//cout<<int(str[i])<<"\t";
count[int(str[i])]=count[int(str[i])]+1;
i++;
}
j=0;
while(j<127)
{
int k=count[j];
while(k>0)
{
c=j;
cout<<c;
k--;
}
//cout<<count[j]<<" "<<" ";
j++;
}
getch();
}
3. Program to display a matrix in spiral way.
ie. if the matrix is
1 2 3
4 5 6
7 8 9
then output will be: 1 2 3 6 9 8 7 4 5
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main()
{
int r,cl,i,j,k;
int mat[4][4];
clrscr();
cout<<"\n enter rows and column of matrix";
cin>>r>>cl;
cout<<"enter elements of matrix";
for(i=0;i<r;i++)
{
for(j=0;j<cl;j++)
{
cin>> mat[i][j];
}
}
cout<<"\n matrix:\n";
for(i=0;i<r;i++)
{
for(j=0;j<cl;j++)
{
cout<< mat[i][j];
cout<<" ";
}
cout<<"\n";
}
int x=0,y=0;
int a=0,b=0,c=r,d=cl;
//cout<<matrix[0][0];
while (a < c && b < d) {
while (y<c) {
cout<<mat[x][y]<<" ";
y++;
}
x++;
y--;
while (x<d) {
cout<<mat[x][y]<<" ";
x++;
}
x--;
y--;
while (y >=a) {
cout<<mat[x][y]<<" ";
y--;
}
y++;
x--;
while (x > b) {
cout<<mat[x][y]<<" ";
x--;
}
y++;
x++;
a++;
b++;
c--;
d--;
}
getch();
}
1. program to copy one file content to another, by removing extra spaces.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
void main() {
FILE *fp1, *fp2;
int i=0;
char ch,ch1;
clrscr();
fp1 = fopen("Sample.txt", "r");
fp2 = fopen("Output.txt", "w");
while (1)
{
ch = fgetc(fp1);
if (ch == EOF)
break;
else if(ch==' ')
{
if(i==1)
{
}
else
{
fputc(' ',fp2);
i=1;
}
}
else
{
fputc(ch, fp2);
i=0;
}
}
printf("File copied Successfully!");
fclose(fp1);
fclose(fp2);
getch();
}
*if u are using turbo c, please do this program by keeping into bin folder, I dont know why but it idnt worked properly on other folers.
2. Program to give output as aabbccee if the input is given as abceacbe.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
main()
{
char str[10];
int i=0,j=0;
int count[127];
while(j<=127)
{
count[j]=0;
j++;
}
clrscr();
cout<<"Enter a string\n";
cin>>str;
cout<<str<<"\n";
char c;
while (str[i]!='\0')
{
//cout<<int(str[i])<<"\t";
count[int(str[i])]=count[int(str[i])]+1;
i++;
}
j=0;
while(j<127)
{
int k=count[j];
while(k>0)
{
c=j;
cout<<c;
k--;
}
//cout<<count[j]<<" "<<" ";
j++;
}
getch();
}
3. Program to display a matrix in spiral way.
ie. if the matrix is
1 2 3
4 5 6
7 8 9
then output will be: 1 2 3 6 9 8 7 4 5
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main()
{
int r,cl,i,j,k;
int mat[4][4];
clrscr();
cout<<"\n enter rows and column of matrix";
cin>>r>>cl;
cout<<"enter elements of matrix";
for(i=0;i<r;i++)
{
for(j=0;j<cl;j++)
{
cin>> mat[i][j];
}
}
cout<<"\n matrix:\n";
for(i=0;i<r;i++)
{
for(j=0;j<cl;j++)
{
cout<< mat[i][j];
cout<<" ";
}
cout<<"\n";
}
int x=0,y=0;
int a=0,b=0,c=r,d=cl;
//cout<<matrix[0][0];
while (a < c && b < d) {
while (y<c) {
cout<<mat[x][y]<<" ";
y++;
}
x++;
y--;
while (x<d) {
cout<<mat[x][y]<<" ";
x++;
}
x--;
y--;
while (y >=a) {
cout<<mat[x][y]<<" ";
y--;
}
y++;
x--;
while (x > b) {
cout<<mat[x][y]<<" ";
x--;
}
y++;
x++;
a++;
b++;
c--;
d--;
}
getch();
}
No comments:
Post a Comment