Sie sind auf Seite 1von 20

Strings

1.
//program to convert uppercase letters of a string to lowercase and vice versa
#include<stdio.h>
#include<ctype.h>
void main()
{
char s[50];
printf("Enter string:\n");
scanf("%s[^\n]",s);
for(int i=0;s[i]!='\0';i++)
{
if(islower(s[i])>0)
s[i]=toupper(s[i]);
else s[i]=tolower(s[i]);
}
printf("the string now is-\n");
puts(s);
}
2.
//program to find frequency of character in a given string
#include<stdio.h>
void main()
{
char str[50],ch;
printf("Enter string:\n");
scanf(" %[^\n]",str);
int freq=0;
printf("Enter the character whose frequency is to be checked:%c",ch);
scanf(" %c",&ch);
for(int i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
freq++;
}
printf("Frequency of %c in the given string=%d",ch,freq);
}
3.
//program to delete consonants from a string
#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
printf("Enter string-");
scanf(" %[^\n]",str);
for(int i = 0;i < strlen(str); i++)
{
if(str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u')
{
for(int j = i; j < strlen(str); j++)
{
str[j] = str[j+1];
}
i--;
}

}
printf("The changed string is:\n%s",str);
}

4.
//program to count different types of characters in given string
#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
printf("Enter string-");
scanf(" %[^\n]",str);
int vow=0,cons=0,dig=0,space=0,special;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||
str[i]=='O'||str[i]=='U')
vow++;
else if((str[i]>'a'&&str[i]<='z')||(str[i]>'A'&&str[i]<='Z'))
cons++;
else if(str[i]>='0'&&str[i]<='9')
dig++;
else if(str[i]==' ')
space++;

}
printf("number of vowels=%d\n",vow);
printf("number of consonants=%d\n",cons);
printf("number of digits=%d\n",dig);
printf("number of spaces=%d\n",space);
}

5.
//program to sort characters of a string
#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
printf("Enter string-");
scanf(" %[^\n]",str);
int l=strlen(str);
char temp;
for(int i=0;i<l;i++)
for(int j=i+1;j<l;j++)
if(str[i]>str[j])
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
printf("the string after sort=%s\n",str);
}
6.
//program to concatenate 2 strings
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int l1,l2,c;
printf("Enter string 1-");
scanf(" %[^\n]",str1);
printf("enter string 2-");
scanf(" %[^\n]",str2);
strcat(str1,str2);
printf("string 1= %s\n",str1);
printf("string 2= %s\n",str2);
}

7.
//program to find length of string using pointers
#include <stdio.h>
void main()
{
char str[100],*pt;
int i = 0;
printf("Enter Any string:\n");
gets(str);
pt=str;
while(*pt!='\0')
{
i++;
pt++;
}
printf("Length of String : %d", i);
}

8.
//program to print initials of any name
#include <stdio.h>
#include <ctype.h>
void main()
{
char s[50];
printf("Enter string:\n");
gets(s);
s[0]=toupper(s[0]);
printf("%c ",s[0]);
int i = 1;
while (s[i] != '\0')
{
if (s[i] ==' ')
{
++i;
s[i]=toupper(s[i]);
printf("%c ",s[i]);
}
else
i++;
}
}

9.
#include<string.h>
#include<stdio.h>
int main(){
char *p;
char s[20],s1[1];
printf("\nEnter a string: ");
scanf("%[^\n]",s);
fflush(stdin);
printf("\nEnter character: ");
gets(s1);
p=strpbrk(s,s1);
printf("\nThe string from the given character is: %s",p);
return 0;
}
10.
//program to reverse a string
#include<stdio.h>
#include<string.h>
void main()
{
char str[50],rev[50];
printf("Enter string:\n");
scanf(" %[^\n]",str);
int l=strlen(str);
int i=0;
for(i=0;i<l;i++)
rev[i]=str[l-i-1];
rev[i]='\0';
printf("The reversed string is:\n%s",rev);
}

11.
//program to convert string to ascii
#include <stdio.h>
#include<string.h>
void main()
{
char s[50];
printf("Enter a string:\n");
scanf(" %[^\n]",s);
printf("The ascii values of the string are:\n");
for (int i = 0; i < strlen(s); i++)
printf("%d ",s[i]);
}

12.
//program to check whether a character is a vowel or consonant
#include<stdio.h>
void main()
{
char ch;
printf("Enter character:\n");
scanf(" %c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("The character is a vowel.");
else if((ch>'a'&&ch<='z')||(ch>'A'&&ch<='Z'))
printf("The character is a consonant.");
else
printf("The character is neither vowel nor consonant.");

13.
//program to check whether a character is an alphabet or not
#include<stdio.h>
void main()
{
char ch;
printf("Enter character:\n");
scanf(" %c",&ch);
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
printf("The character is an alphabet.");
else
printf("The character is not an alphabet.");

14.
//program to print alphabets from A to Z using loop
#include <stdio.h>
void main(){
for (int i = 65; i < 91; i++){
printf("%c ",i);
}
}

15.
//program to count number of vowels,consonants
#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
printf("Enter string:");
scanf(" %[^\n]",str);
int vow=0,cons=0;
for(int i=0;str[i]!='\0';i++)
{

if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||
str[i]=='O'||str[i]=='U')
vow++;
else if((str[i]>'a'&&str[i]<='z')||(str[i]>'A'&&str[i]<='Z'))
cons++;
}
printf("number of vowels=%d\n",vow);
printf("number of consonants=%d\n",cons);
}

16.
//string.h functions
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50],k,*s;
int n,l1,l2,c,ch;
printf("Enter string 1-");
scanf(" %[^\n]",str1);
printf("enter string 2-");
scanf(" %[^\n]",str2);
printf("Choose which function to perform:\n");
printf("1=strlen\n2=strcat\n3=strncat\n4=strcpy\n5=strncpy\n6=strcmp\n7=strncmp\n8=strstr
\n9=strchr\n10=strrchr\n11=strspn\n12=strcspn\n13=strtok\n14=strpbrk\nEnter a choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:l1=strlen(str1);
l2=strlen(str2);
printf("Length of string 1=%d\n",l1);
printf("Length of string 2=%d\n",l2);
break;
case 2:strcat(str1,str2);
printf("string 1= %s\n",str1);
printf("string 2= %s\n",str2);
break;

case 3:printf("Enter the number of characters to be concatenated from string 2 to string 1:");
scanf("%d",&n);
printf("The string 1 now is:\n%s",strncat(str1,str2,n));
break;

case 4:strcpy(str1,str2);
printf("string 1=%s\n",str1);
printf("string 2=%s\n",str2);
break;

case 5:printf("Enter the number of characters to be copied from string 2 to string 1:");
scanf("%d",&n);
printf("The string 1 now is:\n%s",strncpy(str1,str2,n));
break;

case 6:c=strcmp(str1,str2);
if(c==0)
printf("oth the strings are equal.\n");
else if(c>0)
printf("string 1>string 2\n");
else if(c<0)
printf("string 1<string 2\n");
break;

default:printf("Entered wrong choice.\n");

case 7:printf("Enter the number of characters to be compared from string 2 with string 1:");
scanf("%d",&n);
c=strncmp(str1,str2,n);
printf("Uptill the %dth position,",n);
if(c==0)
printf("both the strings are equal.\n");
else if(c>0)
printf("string 1>string 2\n");
else if(c<0)
printf("string 1<string 2\n");
break;

break;

case 8:printf("The first occurrence of string 2 in string 1 is at:\n%s",strstr(str1,str2));


break;

case 9:printf("Enter a character:\n");


scanf(" %c",&k);
printf("The string after first occurrence of %c in string 1 is:\n%s",k,strchr(str1,k));
break;

case 10:printf("Enter a character:\n");


scanf(" %c",&k);
printf("The string after last occurrence of %c in string 1 is:\n%s",k,strrchr(str1,k));
break;

case 11:printf("Length of initial segment of string 1 matching with string


2=%d\n",strspn(str1,str2)); //initital here means starting of the string, checks of much of string
2 is matching with string 1 at the start
break;

case 12:printf("Length of initial segment of string 1 not matching with any of the characters
from string 2=%d\n",strcspn(str1,str2)); //calculates the length of the initial segment of str1,
which consists entirely of characters not in str2.
break;

case 13:printf("Part of string 1 until any of the characters from string 2 is


encountered:\n%s",strtok(str1,str2));
break;
case 14:s=strpbrk(str1,str2);
printf("First character from string 1 that matches any of the characters from string
2:\n%c",*s);
break;
}

Matrix
1.
//program to add 2 matrices
#include <stdio.h>
void main()
{
int m, n;
printf("Enter rows and columns:\n");
scanf("%d %d",&m,&n);
int a[m][n], b[m][n],sum[m][n];
printf("Enter elements for A:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&a[i][j]);
printf("Enter elements for B:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&b[i][j]);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
}
2.
//program to subtract 2 matrices
#include <stdio.h>
void main()
{
int m, n;
printf("Enter rows and columns:\n");
scanf("%d %d",&m,&n);
int a[m][n], b[m][n],diff[m][n];
printf("Enter elements for A:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&a[i][j]);
printf("Enter elements for B:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&b[i][j]);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
diff[i][j] = a[i][j] - b[i][j];
printf("Difference:\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("%d ", diff[i][j]);
printf("\n");
}
}

3.
//program to multiply 2 matrices
#include <stdio.h>

void main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of first matrix\n");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d %d", &p, &q);

if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

for ( c = 0 ; c < p ; c++ )


for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");


for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

printf("\n");
}
}
}

4.
//program to add diagonal elements of matrix
#include <stdio.h>
void main()
{
int m, n;
printf("Enter rows and columns:\n");
scanf("%d %d",&m,&n);
int A[m][n],sum=0;
printf("Enter elements for A:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&A[i][j]);
printf("The matrix\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d\t",A[i][j]);
}
printf("\n");
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(i==j)
sum+=A[i][j];
}
printf("The sum of the diagonal elements are:%d",sum);
}

5.
//program to transpose a matrix
#include <stdio.h>
void main()
{
int m, n;
printf("Enter rows and columns:\n");
scanf("%d %d",&m,&n);
int A[m][n],T[n][m];
printf("Enter elements for A:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&A[i][j]);
printf("The matrix\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
T[i][j]=A[j][i];
printf("The transpose of the matrix is:\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
printf("%d\t",T[i][j]);
printf("\n");
}

6.
//program to find inverse of matrix
#include<stdio.h>

void main()
{
int mat[3][3], i, j;
float determinant = 0;

printf("Enter elements of matrix row wise:\n");


for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &mat[i][j]);

printf("\nGiven matrix is:");


for(i = 0; i < 3; i++){
printf("\n");

for(j = 0; j < 3; j++)


printf("%d\t", mat[i][j]);
}
for(i = 0; i < 3; i++)
determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] -
mat[1][(i+2)%3] * mat[2][(i+1)%3]));

printf("\n\ndeterminant: %f\n", determinant);

printf("\nInverse of matrix is: \n");


for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
printf("%.2f\t",((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) -
(mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant);

printf("\n");
}
}

7.
//program to print lower triangular matrix
#include <stdio.h>
void main()
{
int m, n;
printf("Enter rows and columns:\n");
scanf("%d %d",&m,&n);
int A[m][n];
printf("Enter elements for A:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&A[i][j]);
printf("The lower triangular matrix:\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(j<=i)
printf("%d\t",A[i][j]);
}
printf("\n");
}
}
8.
//program to print upper triangular matrix
#include <stdio.h>
void main()
{
int m, n;
printf("Enter rows and columns:\n");
scanf("%d %d",&m,&n);
int A[m][n];
printf("Enter elements for A:\n");
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d",&A[i][j]);
printf("The upper triangular matrix:\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i<=j)
printf("%d\t",A[i][j]);
}
printf("\n");
}
}
9.
//program to find determinant of matrix
#include<stdio.h>

void main()
{
int mat[3][3], i, j;
float determinant = 0;

printf("Enter elements of matrix row wise:\n");


for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &mat[i][j]);

printf("\nGiven matrix is:");


for(i = 0; i < 3; i++){
printf("\n");

for(j = 0; j < 3; j++)


printf("%d\t", mat[i][j]);
}
for(i = 0; i < 3; i++)
determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] -
mat[1][(i+2)%3] * mat[2][(i+1)%3]));

printf("\n\ndeterminant: %f\n", determinant);

10. question is same as the 5th one


11.
//program to multiply two matrices by passing matrix to function
#include<stdio.h>
void mprint(int r,int c,int a[r][c]);
void minput(int r,int c,int a[r][c]);
void multi(int r1,int c1,int r2,int c2,int a[r1][c1],int b[r2][c2],int res[r1][c2]);
void main()
{
int rowA,colA,rowB,colB;
printf("Enter the number of rows and columns of matrix A-\n");
scanf("%d %d",&rowA,&colA);
printf("Enter the number of rows and columns of matrix B-\n");
scanf("%d %d",&rowB,&colB);
int A[rowA][colA],B[rowB][colB],C[rowA][colB];
printf("Enter the elements of matrix A-\n");
minput(rowA,colA,A);
printf("Enter the elements of matrix B-\n");
minput(rowB,colB,B);
printf("Matrix A:\n");
mprint(rowA,colA,A);
printf("Matrix B:\n");
mprint(rowB,colB,B);
multi(rowA,colA,rowB,colB,A,B,C);
}

void mprint(int r,int c,int a[r][c])


{
for(int i=0;i<r;i++)
{

for(int j=0;j<c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void minput(int r,int c,int a[r][c])
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
void multi(int r1,int c1,int r2,int c2,int a[r1][c1],int b[r2][c2],int res[r1][c2])
{
int sum=0;
if(c1==r2)
{
for(int i=0;i<r1;i++)
{

for(int j=0;j<c2;j++)
{

for(int k=0;k<c1;k++)

sum+=(a[i][k]*b[k][i]);

res[i][j]=sum;
sum=0;
}
}
printf("Result of multiplication of the two matrices-\n");
mprint(r1,c2,res);
}
else printf("The matrices can't be multiplied with each other.\n");
}

Das könnte Ihnen auch gefallen