STRING LAB REPORT
Q1. Write a program that will read a string and show each
character with one space.
 #include<stdio.h>
 int main ()
 {
 char ch[1000];
 int i;
 printf("ENTER A STRING:n");
 gets(ch);
 printf("WITH SPACE : n");
 for(i=0;ch[i]!='0';i++)
 {
 printf("%c ",ch[i]);
 }
 return 0;
 }
Q2. Write a program that will read a string and show each
character with one space in reverse order.
 #include<stdio.h>
 int main ()
 {
 char ch[1000];
 int i,j;
 printf("ENTER A STRING:n");
 gets(ch);
 printf("REVERS PRINT WITH SPACE : n");
 for(i=0;ch[i]!='0';i++);
 for(j=i-1;j>=0;j--)
 {
 printf("%c ",ch[j]);
 }
 return 0;
 }
Q3. Write a program that will read a string and show each
character in separate line.
 #include<stdio.h>
 int main ()
 {
 char ch[1000];
 int i;
 printf("ENTER A STRING:n");
 gets(ch);
 printf("EACH CHARACTER IN SEPARATE LINE: n");
 for(i=0;ch[i]!='0';i++)
 {
 printf("%cn",ch[i]);
 }
 return 0;
 }
Q4. Write a program that will read a string and show each
character and corresponding ASCII value in separate line.
 #include<stdio.h>
 int main ()
 {
 char ch[1000];
 int i;
 printf("ENTER A STRING:n");
 gets(ch);
 printf("EACH CHARACTER IN SEPARATE LINE WITH CORRESPONDING
ASCII VALUE: n");
 for(i=0;ch[i]!='0';i++)
 {
 printf("%ct%dn",ch[i],ch[i]);
 }
 return 0;
 }
Q5. Write a program that will read a string and show frequency of
every character in this string.
 #include<stdio.h>
 int main ()
 {
 char ch[1000];
 int i,count[26]={0};
 printf("ENTER A STRING:n");
 gets(ch);
 for(i=0;ch[i]!='0';i++)
 {
 if(ch[i]>='a'&&ch[i]<='z')
 count[ch[i]-'a']++;
 }
 for(i=0;i<26;i++)
 {
 if(count[i]!=0)
 printf("%c OCCURS %d TIME(S):n",i+'a',count[i]);
 }
 return 0;
 }
Q6. Write a program that will read a string and show the length of
that string.
 #include<stdio.h>
 int main ()
 {
 char ch[1000];
 int i;
 printf("ENTER A STRING:n");
 gets(ch);
 for(i=0;ch[i]!='0';i++);
 printf("LENGTH OF THE STRING IS : %d",i);
 return 0;
 }
Q7. Write a program that will read a string and show the string in
reverse order.
 #include<stdio.h>
 int main ()
 {
 char ch[1000];
 int i,j;
 printf("ENTER A STRING:n");
 gets(ch);
 printf("REVERS PRINT : n");
 for(i=0;ch[i]!='0';i++);
 for(j=i-1;j>=0;j--)
 {
 printf("%c",ch[j]);
 }
 return 0;
 }
Q8. Write a program that will read a string and copy this to
another string.
 #include<stdio.h>
 int main ()
 {
 char ch[100],st[100];
 int i,j;
 printf("ENTER A STRING:n");
 gets(ch);
 for(i=0;ch[i]!='0';++i)
 {
 st[i]=ch[i];
 }
 st[i]='0';
 puts(st);
 return 0;
 }
Q9. Write a program that will read two strings and add second
string with first string.
 #include<stdio.h>
 int main ()
 {
 char ch[100],st[100];
 printf("ENTER FIRST STRING:n");
 gets(ch);
 printf("ENTER SECOND STRING:n");
 gets(st);
 strcat(ch,st);
 printf("n%s",ch);
 return 0;
 }
Q10. Write a program that will read two strings and compare
which one is greater.
 #include<stdio.h>
 int main ()
 {
 char ch[1000],st[1000];
 int i,j;
 printf("ENTER FIRST STRING:n");
 gets(ch);
 for(i=0;ch[i]!='0';i++);
 printf("ENTER SECOND STRING:n");
 gets(st);
 for(j=0;st[j]!='0';j++);
 if(i>j) printf("STRING 1 IS GREATER");
 else printf("STRING 2 IS GREATER");
 return 0;
 }
Q11. Write a program that will read a string and convert all
lowercase letter to uppercase and all uppercase letter to
lowercase.
 #include<stdio.h>
 int main ()
 {
 int c;
 char ch,s[1000];
 printf("ENTER A STRINGn");
 gets(s);
 for(c=0;s[c]!='0';c++)
 {
 ch=s[c];
 if(ch >= 'A' && ch <= 'Z')
 s[c]= s[c]+32;
 else if(ch >= 'a' && ch <= 'z')
 s[c]= s[c]-32;
 }
 printf("%sn",s);
 return 0;
 }
FUNCTION PROBLEM
Q1. Write a function that gets two integers and returns sum.
 #include<stdio.h>
 int sum(int a, int b)
 {
 int result;
 result=a+b;
 return result;
 }
 int main ()
 {
 int x,y,res;
 printf("ENTER TWO NUMBERS:n");
 scanf("%d %d",&x,&y);
 res=sum(x,y);
 printf("SUM OF %d AND %d IS %d",x,y,res);
 return 0;
 }
Q2. Write a function that gets two integers and returns division.
 #include<stdio.h>
 int sum(int a, int b)
 {
 int result;
 result=a+b;
 return result;
 }
 int main ()
 {
 int x,y,res;
 printf("ENTER TWO NUMBERS:n");
 scanf("%d %d",&x,&y);
 res=sum(x,y);
 printf("SUM OF %d AND %d IS %d",x,y,res);
 return 0;
 }
Q3. Write a function that gets length and width of a
rectangle and returns area
 #include<stdio.h>
 int result;
 int mul(int a, int b)
 {

 result=a*b;
 return result;
 }
 int main ()
 {
 int x,y;
 printf("ENTER LENGTH OF THE RECTANGLE:n");
 scanf("%d",&x);
 printf("ENTER HIGHT OF THE RECTANGLE:n");
 scanf("%d",&y);
 mul(x,y);
 printf("AREA OF THE RECTANGLE IS %d SQUARE UNIT",result);
 return 0;
 }
Q4. Write a function that gets radius of a circle and
returns area.
 #include<stdio.h>


 void ara(float x)
 {
 float result;
 float pi=3.1416;
 result=pi*x*x;
 printf("AREA OF THE CIRCLE IS %.3f SQUARE UNIT",result);
 }
 int main ()
 {
 float r;
 printf("ENTER THE RADIOUS OF THE CIRCLE :n");
 scanf("%f",&r);
 ara(r);

 return 0;
 }
Q5. Write a function that gets any positive integer and
returns its factorial.
 #include<stdio.h>

 int fac(int x)
 {
 int i,j=1;
 for (i=1;i<=x;i++)
 {

 j=j*i;

 }
 return j;
 }
 int main ()
 { int y,z;
 printf("ENTER A NUMBERn");
 scanf("%d",&y);
 if(y>=0)
 {
 z=fac(y);
 printf("FACTORIAL OF %d IS %d",y,z);
 }
 else printf("Number is negative");
 return 0;
 }
Q6. Write a function that gets any positive integer and
returns its reverse
 #include<stdio.h>
 int revrs(int num)
 {
 int reminder,reverse=0,res;
 while(num>0)
 {
 reminder = num%10;
 num = num/10;
 reverse = reverse*10+reminder;

 }
 return reverse;
 }
 int main()
 {
 int a,res;
 printf("Enter a positive integer number: ");
 scanf("%d",&a);
 if(a<0)
 {
 printf("Number is negativen");
 } else
 {
 res = revrs(a);
 printf("%d",res);
 }
 return 0;
 }
Q7. Write a function that gets two positive integers and
returns LCM (Least Common Multiple).
 #include<stdio.h>
 void lcm(int x,int y)
 {
 int i=1;

 while(i>0)
 {
 if(i%x==0 && i%y==0)
 {
 printf("THE LCM OF %d AND %d IS %d",x,y,i);
 break;

 }i++;
 }
 }
 int main ()
 {
 int p,q;
 scanf("%d %d",&p,&q);
 lcm(p,q);
 }
Q8. Write a function that gets two positive integers and
returns nCr.
 #include<stdio.h>
 int fac(int x)
 {
 int i,j=1;
 for (i=1;i<=x;i++)
 {

 j=j*i;

 }
 return j;
 }

 int main()
 {
 int n,m,o,p,q,r,s;
 printf("ENTER n:n");
 scanf("%d",&n);
 printf("ENTER r:n");
 scanf("%d",&r);
 o=fac(r);
 m=fac(n);
 p=(n-r);
 q=fac(p);
 if(n>=0 && r >=0 && n>=r){
 s=m/(q*o);
 printf("%dC%d = %d",n,r,s);
 }
 else printf("MATH ERROR");
 }
Q9. Write a function that gets any positive integer and
determine prime or not prime.
 #include<stdio.h>
 int prime=0;
 void pri(int x)
 {
 int i;
 for(i=2;i<=x/2;i++)
 {
 if(x%i==0)
 {
 prime=1;
 break;
 }
 }
 if(prime==0) printf("%d IS A PRIME NUMBER",x);
 else printf("%d IS NOT A PRIME NUMBER",x);


 }
 int main()
 {
 int n;

 scanf("%d",&n);
 pri(n);

 }

C programming

  • 2.
  • 3.
    Q1. Write aprogram that will read a string and show each character with one space.  #include<stdio.h>  int main ()  {  char ch[1000];  int i;  printf("ENTER A STRING:n");  gets(ch);  printf("WITH SPACE : n");  for(i=0;ch[i]!='0';i++)  {  printf("%c ",ch[i]);  }  return 0;  }
  • 4.
    Q2. Write aprogram that will read a string and show each character with one space in reverse order.  #include<stdio.h>  int main ()  {  char ch[1000];  int i,j;  printf("ENTER A STRING:n");  gets(ch);  printf("REVERS PRINT WITH SPACE : n");  for(i=0;ch[i]!='0';i++);  for(j=i-1;j>=0;j--)  {  printf("%c ",ch[j]);  }  return 0;  }
  • 5.
    Q3. Write aprogram that will read a string and show each character in separate line.  #include<stdio.h>  int main ()  {  char ch[1000];  int i;  printf("ENTER A STRING:n");  gets(ch);  printf("EACH CHARACTER IN SEPARATE LINE: n");  for(i=0;ch[i]!='0';i++)  {  printf("%cn",ch[i]);  }  return 0;  }
  • 6.
    Q4. Write aprogram that will read a string and show each character and corresponding ASCII value in separate line.  #include<stdio.h>  int main ()  {  char ch[1000];  int i;  printf("ENTER A STRING:n");  gets(ch);  printf("EACH CHARACTER IN SEPARATE LINE WITH CORRESPONDING ASCII VALUE: n");  for(i=0;ch[i]!='0';i++)  {  printf("%ct%dn",ch[i],ch[i]);  }  return 0;  }
  • 7.
    Q5. Write aprogram that will read a string and show frequency of every character in this string.  #include<stdio.h>  int main ()  {  char ch[1000];  int i,count[26]={0};  printf("ENTER A STRING:n");  gets(ch);  for(i=0;ch[i]!='0';i++)  {  if(ch[i]>='a'&&ch[i]<='z')  count[ch[i]-'a']++;  }  for(i=0;i<26;i++)  {  if(count[i]!=0)  printf("%c OCCURS %d TIME(S):n",i+'a',count[i]);  }  return 0;  }
  • 8.
    Q6. Write aprogram that will read a string and show the length of that string.  #include<stdio.h>  int main ()  {  char ch[1000];  int i;  printf("ENTER A STRING:n");  gets(ch);  for(i=0;ch[i]!='0';i++);  printf("LENGTH OF THE STRING IS : %d",i);  return 0;  }
  • 9.
    Q7. Write aprogram that will read a string and show the string in reverse order.  #include<stdio.h>  int main ()  {  char ch[1000];  int i,j;  printf("ENTER A STRING:n");  gets(ch);  printf("REVERS PRINT : n");  for(i=0;ch[i]!='0';i++);  for(j=i-1;j>=0;j--)  {  printf("%c",ch[j]);  }  return 0;  }
  • 10.
    Q8. Write aprogram that will read a string and copy this to another string.  #include<stdio.h>  int main ()  {  char ch[100],st[100];  int i,j;  printf("ENTER A STRING:n");  gets(ch);  for(i=0;ch[i]!='0';++i)  {  st[i]=ch[i];  }  st[i]='0';  puts(st);  return 0;  }
  • 11.
    Q9. Write aprogram that will read two strings and add second string with first string.  #include<stdio.h>  int main ()  {  char ch[100],st[100];  printf("ENTER FIRST STRING:n");  gets(ch);  printf("ENTER SECOND STRING:n");  gets(st);  strcat(ch,st);  printf("n%s",ch);  return 0;  }
  • 12.
    Q10. Write aprogram that will read two strings and compare which one is greater.  #include<stdio.h>  int main ()  {  char ch[1000],st[1000];  int i,j;  printf("ENTER FIRST STRING:n");  gets(ch);  for(i=0;ch[i]!='0';i++);  printf("ENTER SECOND STRING:n");  gets(st);  for(j=0;st[j]!='0';j++);  if(i>j) printf("STRING 1 IS GREATER");  else printf("STRING 2 IS GREATER");  return 0;  }
  • 13.
    Q11. Write aprogram that will read a string and convert all lowercase letter to uppercase and all uppercase letter to lowercase.  #include<stdio.h>  int main ()  {  int c;  char ch,s[1000];  printf("ENTER A STRINGn");  gets(s);  for(c=0;s[c]!='0';c++)  {  ch=s[c];  if(ch >= 'A' && ch <= 'Z')  s[c]= s[c]+32;  else if(ch >= 'a' && ch <= 'z')  s[c]= s[c]-32;  }  printf("%sn",s);  return 0;  }
  • 14.
  • 15.
    Q1. Write afunction that gets two integers and returns sum.  #include<stdio.h>  int sum(int a, int b)  {  int result;  result=a+b;  return result;  }  int main ()  {  int x,y,res;  printf("ENTER TWO NUMBERS:n");  scanf("%d %d",&x,&y);  res=sum(x,y);  printf("SUM OF %d AND %d IS %d",x,y,res);  return 0;  }
  • 16.
    Q2. Write afunction that gets two integers and returns division.  #include<stdio.h>  int sum(int a, int b)  {  int result;  result=a+b;  return result;  }  int main ()  {  int x,y,res;  printf("ENTER TWO NUMBERS:n");  scanf("%d %d",&x,&y);  res=sum(x,y);  printf("SUM OF %d AND %d IS %d",x,y,res);  return 0;  }
  • 17.
    Q3. Write afunction that gets length and width of a rectangle and returns area  #include<stdio.h>  int result;  int mul(int a, int b)  {   result=a*b;  return result;  }  int main ()  {  int x,y;  printf("ENTER LENGTH OF THE RECTANGLE:n");  scanf("%d",&x);  printf("ENTER HIGHT OF THE RECTANGLE:n");  scanf("%d",&y);  mul(x,y);  printf("AREA OF THE RECTANGLE IS %d SQUARE UNIT",result);  return 0;  }
  • 18.
    Q4. Write afunction that gets radius of a circle and returns area.  #include<stdio.h>    void ara(float x)  {  float result;  float pi=3.1416;  result=pi*x*x;  printf("AREA OF THE CIRCLE IS %.3f SQUARE UNIT",result);  }  int main ()  {  float r;  printf("ENTER THE RADIOUS OF THE CIRCLE :n");  scanf("%f",&r);  ara(r);   return 0;  }
  • 19.
    Q5. Write afunction that gets any positive integer and returns its factorial.  #include<stdio.h>   int fac(int x)  {  int i,j=1;  for (i=1;i<=x;i++)  {   j=j*i;   }  return j;  }  int main ()  { int y,z;  printf("ENTER A NUMBERn");  scanf("%d",&y);  if(y>=0)  {  z=fac(y);  printf("FACTORIAL OF %d IS %d",y,z);  }  else printf("Number is negative");  return 0;  }
  • 20.
    Q6. Write afunction that gets any positive integer and returns its reverse  #include<stdio.h>  int revrs(int num)  {  int reminder,reverse=0,res;  while(num>0)  {  reminder = num%10;  num = num/10;  reverse = reverse*10+reminder;   }  return reverse;  }  int main()  {  int a,res;  printf("Enter a positive integer number: ");  scanf("%d",&a);  if(a<0)  {  printf("Number is negativen");  } else  {  res = revrs(a);  printf("%d",res);  }  return 0;  }
  • 21.
    Q7. Write afunction that gets two positive integers and returns LCM (Least Common Multiple).  #include<stdio.h>  void lcm(int x,int y)  {  int i=1;   while(i>0)  {  if(i%x==0 && i%y==0)  {  printf("THE LCM OF %d AND %d IS %d",x,y,i);  break;   }i++;  }  }  int main ()  {  int p,q;  scanf("%d %d",&p,&q);  lcm(p,q);  }
  • 22.
    Q8. Write afunction that gets two positive integers and returns nCr.  #include<stdio.h>  int fac(int x)  {  int i,j=1;  for (i=1;i<=x;i++)  {   j=j*i;   }  return j;  }   int main()  {  int n,m,o,p,q,r,s;  printf("ENTER n:n");  scanf("%d",&n);  printf("ENTER r:n");  scanf("%d",&r);  o=fac(r);  m=fac(n);  p=(n-r);  q=fac(p);  if(n>=0 && r >=0 && n>=r){  s=m/(q*o);  printf("%dC%d = %d",n,r,s);  }  else printf("MATH ERROR");  }
  • 23.
    Q9. Write afunction that gets any positive integer and determine prime or not prime.  #include<stdio.h>  int prime=0;  void pri(int x)  {  int i;  for(i=2;i<=x/2;i++)  {  if(x%i==0)  {  prime=1;  break;  }  }  if(prime==0) printf("%d IS A PRIME NUMBER",x);  else printf("%d IS NOT A PRIME NUMBER",x);    }  int main()  {  int n;   scanf("%d",&n);  pri(n);   }