Posts Tagged C code

Write a program that replace two or more blank spaces by single blank.

e.g. ” i    hate   my          books”
output:: “i hate my books”

#include<stdio.h>

void blank(char *str)
{
int i=0;
char *temp,timepass[100];
temp=timepass;

while(*str!='\0')
{ if(*str !=' ')
{ *temp=*str;
temp++;
}
else
{ if(*(str+1) != ' ')
{ *temp=*str;
temp++;
}
}

str++;
}

*temp='\0';
puts(" ");
printf("\n%s",timepass);
puts(" ");
}

main()
{
char str[100];
puts("Enter a string:: ");
gets(str);
blank(str);
}

, , ,

4 Comments

Program to sort elements of a matrix.

#include<stdio.h>

sorting(int *a, int n,int m)
{ int l,i,j,min,temp;
l=n*m;
for(i=0;i<l;i++)
{ min= *(a+i);
for(j=i;j<l;j++)
{ if(min > *(a+j))
{ min=*(a+j);
*(a+j)=*(a+i);
*(a+i)=min;
}
}
}

}

main()
{ int i,k=0,j,a[2][2],min=0;
printf("Enter the elements of matrix:: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);

printf("\nOriginal Array is:: ");
for(i=0;i<2;i++)
{ printf("\n");
for(j=0;j<2;j++)
printf("\t%d",a[i][j]);
}

sorting(a,2,2);

printf("\nSorted Array is:: ");
for(i=0;i<2;i++)
{ printf("\n");
for(j=0;j<2;j++)
printf("\t%d",a[i][j]);
}
  puts("");

}

, , ,

1 Comment

Implementation of function “atoi()” and sum of the converts integers digites.

#include<stdio.h>
#include<string.h>
main()
{ int i,n,l,j=0,sum=0;
char ch,str[10],*p;
puts("Enter the string which you want to convert in interger::");
gets(str);
l=strlen(str);
p=str;
int a[l];

while((*p)!='\0' && j<l)
{ //printf("\nc=%c\n",*p);
i=*p-48;
//printf("\nd=%d\n",i);
a[j]=i;
j++;
p++;
}

printf("\nThe integer is:: ");
for(i=0; i<l;i++)
printf("%d",a[i]);
printf("\nSum of the digites of the integer::\n");
for(i=0;i<l;i++)
sum+=a[i];
printf("Sum= %d\n",sum);
}

, , ,

No Comments

Write a program to create a Pascal triangle.

#include<stdio.h>
main()
{  int i,j,k,n;
printf("\nEnter number of rows::");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ for(k=1;k<=(n-i);k++)
printf(" ");
for(j=1;j<=i;j++)
printf(" %d",j);
for(j=i-1; j>0; j--)
printf(" %d",j);
printf("\n");
}
}

,

No Comments

Write a program to creat two triangle of alphabets.

A B C D E F G F E D C B A
A B C D E F    F E D C B A
A B C D E         E  D C B A
A B C D               D C B A
A B C                     C B A
A B                           B A
A                                 A

#include<stdio.h>
main()
{
int i,j,k,n;
printf("\nEnter the number of rows::");
scanf("%d",&n);

for(i=0;i<n;i++ )
{ for(j=65;j< 65+n-i; j++)
printf("%c ",j);

for(k=1;k<(4*i-1);k++)
printf(" ");

if(i==0) printf("\b\b");

for(j=j-1;j>=65;j--)
printf("%c ",j);

printf("\n");
}
}

, ,

1 Comment

Write a program that converts lower case to uper case and vice versa of a string.

#include<stdio.h>
main()
{
int i=0;
char ch, str[100];
puts("\nEnter the string");
gets(str);
puts("Origial string:: ");
puts(str);
while(str[i]!='\0')
{if(str[i]>=65 && str[i]<=90)
str[i]+=32;
else if(str[i]>=97 && str[i]<=122)
str[i]-=32;
i++;
}
puts("New string:: ");
puts(str);
}

, , ,

1 Comment

write a program to find a square matrix is symmetric.

#include<stdio.h>
main()
{ int i,j,a[10][10],b[10][10],n,temp;
printf("\nEnter the dimensions of matrix:: ");
scanf("%d",&n);
printf("\nEnter the eleents of matrix::\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("\nOriginal Matrix is:: ");
for(i=0;i<n;i++)
{ printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}

for(i=0;i<n;i++)
for(j=0;j<n;j++)
{b[j][i]=a[i][j];
}

printf("\nTranspose Matrix is:: ");
for(i=0;i<n;i++)
{ printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}

for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{if(a[i][j]!=b[i][j])
{ printf("\nMatrix is not symmatric  :(\n");
exit(0);
}
}
}
printf("\nMatrix is symmatric  :)\n");
}

, ,

1 Comment

Write a program that extract a sub string from a given position.

/* Write a program that extract a sub string from a given position.
eg. “Working with string is fun” , then if from positoin 4, and 4 characters to be extracted,
that is “king”. If the number of charcters  to be extracted is 0 or negative then the program should
return whole string.
*/

#include<stdio.h>
main()
{
int i,n;
char str[100];
puts("Enter the string:: ");
gets(str);
printf("\nEnter the position from you wanna extract string:: ");
scanf("%d",&n);
if(n==0 || n<0) puts(str);
else for(i=n;i<n+n;i++)
printf("%c",str[i]);
}

, ,

No Comments

Write a program to find the norm of a matrix.

/* Norm is the sum of the squars of all the elements of the matrix. */

#include<stdio.h>
#include<math.h>
main()
{ int i,j,n,m,a[10][10],sum=0;
double norm;
printf("\nEnter the dimensions:: ");
scanf("%d%d",&n,&m);

printf("\nEnter the eleents of matrix::\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);

printf("\nOriginal Matrix is:: ");
for(i=0;i<n;i++)
{ printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}

for(i=0;i<n;i++)
for(j=0;j<m;j++)
{ sum+=(a[i][j] * a[i][j]);
}

norm=sqrt((double)sum);

printf("\nNORM:: %lf", norm);
}

, , ,

No Comments

Write a program that takes input the X and Y coordinate of 10 differet points and find the total distance between first and last point that is the sum of distances between two consecutive points.

#include<stdio.h>
#include<math.h>
main()
{ int i,sqr;
double x[10],y[10],x1,y1 ,dist, sum=0.0;
printf("\nEnter the coordinates of 3 points,in the form (X,Y):: ");
for(i=0;i<10;i++)
scanf("%lf %lf",&x[i],&y[i]);
for(i=0;i<9;i++)
{ x1=pow((x[i+1]-x[i]),2.0);
y1=pow((y[i+1]-y[i]),2.0);
dist=sqrt(x1+y1);
sum+=dist;
}
printf("\nDistance is= %lf\n",sum);
}

, , , , ,

No Comments