Digital Learning

Friday, April 17, 2020

'C' Programs related to String & Matrix:

1. Write a C program to enter a string and print the same.
2. Write a C program to find out number of letters in a string.
3. Write a C program to check that you entered alphabet, digit or special character and print the same.
4. Write a C program to convert small letters into capital letters.
5. Write a C program to check that the given string is palindrome or not.
6. Write a C program for 3*3 matrix, enter the values into matrix and print the same.
7. Write a C program to enter values in a 4*4 matrix and print matrix that divisible by 2.
8. Write a C program to find out maximum and minimum matrix.

Program: Write a C program to enter a string and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
char name[10];
clrscr();
printf("\n Enter name");
scanf("%s", &name);
while(name[i]!='\0')
{
printf("%c", name[i]);
i++;
}
getch();
}
Output:
Enter name FARAZ
FARAZ

     
Program: Write a C program to find out number of letters in a string.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0, a=0;
char name[10];
clrscr();
printf("\n Enter name");
scanf("%s", &name);
while(name[i]!='\0')
{
a=a+1;
i++;
}
printf("No. of letters=%d", a);
getch();
}
Output:
Enter name 
FARAZ
Result=5
     
Program: Write a C program to check that you entered alphabet, digit or special character and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int alpha=0, digit=0, sc=0, i=0;
char str[20];
clrscr();
printf("\n Enter string");
scanf("%s", &str[i]);
while(str[i]!='\0')
{
if(str[i]>='a' && str[i]<='z')
alpha++;
else
if(str[i]>='0' && str[i]<='9')
digit++;
else
sc++;
i++;
}
printf("\n Alphabet=%d", alpha);
printf("\n Digit=%d", digit);
printf("\n Special character=%d", sc);
getch();
}
Outptu:
Enter string
a
Alphabet=a
Enter string 
5
Digit=5
Enter string
@
Special character=@

Program: Write a C program to convert small letters into capital letters.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
char str[20];
clrscr();
printf('\n Enter string");
scanf("%s", &str[i]);
while(str[i]!='\0')
{
str[i]=str[i]-32;
i++;
}
printf("Converted String=%s", str);
getch();
}
Output:
Enter string faraz
Converted String=FARAZ

   
Program: Write a C program to check that the given string is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0, j=0;
char str[20];
clrscr();
printf("\n Enter string");
scanf("%s", &str[i]);
while(str[i]!='\0')
{
i++;
}
i--;
while(i>=0)
{
if(str[i]!=str[j])
break;
i--;
j++;
}
if(i>=0)
printf("Not Palindrome");
else
printf('Palindrome");
getch();
}
Output:
Enter string nitin
Palindrome

Program: Write a C program for 3*3 matrix, enter the values into matrix and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[3][3], i,j;
clrscr();
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\n enter values");
scanf("%d", &mat[i][j]);
}
}
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d", mat[i][j]);
printf("   ");
}
printf("\n");
}
getch();
}
Output:
Enter values 1
2
3
4
5
6
7
8
9
1  2  3
4  5  6
7  8  9

 
             
Program: Write a C program to enter values in a 4*4 matrix and print matrix that divisible by 2.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[4][4], i,j;
clrscr();
for(i=0;i<4; i++)
{
for(j=0; j<4; j++)
{
printf("\n enter values");
scanf("%d", &mat[i][j]);
}
}
for(i=0;i<4; i++)
{
for(j=0; j<4; j++)
{
if(mat[i][j]%2==0)
printf("%d", mat[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter values 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8
1 1 1 1 
2 2 2 2
3 3 3 3
4 4 4 4

Program: Write a C program to find out maximum and minimum matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[3][3], i,j,max,min;
clrscr();
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\n enter values");
scanf("%d", &mat[i][j]);
}
}
max=min=mat[0][0];
for(i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
if(max<mat[i][j])
max=mat[i][j];
if(min>mat[i][j])
min=mat[i][j];
}
}
printf("%d", max);
printf("%d", min);
printf("\n");
getch();
}

No comments:

Post a Comment