#include <stdio.h>
int main()
{
char name [10];
strcpy (name, "Anywhere");
printf ( "%c%c%c", name [4],name (5), name [6] );
}
Sample Questions and answers
1. Program to find the length of a string
#include <stdio.h>
int main ()
{
int count;
char name [20]:
printf ("Enter any string :
");
scanf("%s" name);
count = 0;
while(name[count] != '")
{
count++;
}
printf("The length of the string is %d
", count);
}
2. Program to compare the length of two strings
#include <stdio.h>
int main ()
{
int count, flag;
char name1 [20], name2 [20];
printf ( "Enter the first string :
");
scanf ("%s", name1 ) ;
print£ ("Enter the second string :
");
scanf ("%s", name2);
for (count=0, flag=0; name1 [count ] != ''; count ++)
{
if (name1 [count] = = name2 [count])
{
flag = 0;
}
else
if(name1 [count] > name2 [count])
{
flag = 1;
break;
}
else
if(name1 [count] < name2 [count])
{
flag = -1;
break;
}
}
if(flag == 0)
{
printf("Both the strings are of equal length
");
}
else
if(flag == 1)
{
printf("The first string is bigger
");
}
else
if(flag == -1)
{
printf("The second string is bigger
");
}
}
3. Program to do matriN addition
#include <stdio.h>
int main ()
{
int a[3][3], b[3][3], c[3][3];
int j, k;
printf ("Enter the first matrix
");
for (j =0; j<3; j++)
{
for(k=0; k<3; k++)
{
scanf("%d", &a[j][k]);
}
}
printf("Enter the second matrix
");
for (j =0; j<3; j++)
{
for(k=0; k<3; k++)
{
scanf("%d", &b[j][k]);
}
}
for (j =0; j<3; j++)
{
for(k=0; k<3; k++)
{
C[j][k] = a[j][k] + b[j][k];
}
}
printf("The sum of two matrices A and B is
");
for (j =0; j<3; j++)
{
for(k=0; k<3; k++)
{
printf("%d ", c[j][k]);
}
}
}
4 . Program to sort numbers in ascending and descending order
#include <stdio.h>
int main()
{
int temp;
int a[20],i,j,k;
printf ("Enter the numbers
"):
for (i=0; i < 20; i++)
{
scanf ( "%d", &a[i] );
}
for(j=0; j < i-1; j++)
{
for(k=j+1; k < i; k++)
{
if(a[j] > a[k])
{
temp = a[j];
a[j] = a[k];
a[k] = temp;
}
}
}
printf("The sorted number in ascending order are
");
for(i=0; i<20; i++)
{
printf("%d
", a[i]);
}
printf("The sorted number in descending order are
");
for(i=19; i!= -1; i--)
{
printf("%d
", a[i]);
}
}
5. Program to sot numbers in ascending order
#include <stdio.h>
int main()
{
int temp;
int a[20],i,j, k;
printf ("Enter the numbers
");
for (i=0; i < 20; i++)
{
scanf ( "%d",&a[i]);
}
for(j=0; j < 20; j++)
{
for (k=j+1; k < 20 ; k++){
if (a[j] > a[k])
{
temp = a[j];
a[j] = a[k];
a[k] = temp;
}
}
}
printf("The sorted numbers in ascending order are
");
for (i=0; i < 20; i++)
{
printf ( "%d
",a[i]);
}
}
6. Program to sot numbers in descending order
#include <stdio.h>
int main()
{
int temp;
int a[20],i,j, k;
printf ("Enter the numbers
");
for (i=0; i < 20; i++)
{
scanf ( "%d",&a[i]);
}
for(j=0; j < 20; j++)
{
for (k=j+1; k < 20; k++)
{
if (a[j] > a[k])
{
temp = a[j];
a[j] = a[k];
a[k] = temp;
}
}
}
printf("The sorted numbers in descending order are
");
for (i=0; i < 20; i++)
{
printf ( "%d
",a[i]);
}
}
7. Program to reverse a given string
#include <stdio.h>
int main()
{
char name_[20],name2 [20];
int i, j;
printf ( "Enter any string
");
gets(name1);
for (i=0; name1[i] !=""; i++);
i--;
for (J=0; i!= -1; j++,i--)
name2[j] = name1[i];
name2[i] = '';
printf ("The reversed string is
");
puts (name2);
}