CSE Assignment -2(Abeer) (1)
CSE Assignment -2(Abeer) (1)
Assignment: 2
Course Code: CSE 111
Course: Structured Programming Language
Solution:
#include<stdio.h>
int main(){
int r,c,a[2][3],b[2][3],sum[2][3],i,j;
printf("The number of rows:");
scanf("%d",&r);
printf("The number of columns:");
scanf("%d",&c);
printf("\nValues of matrix A:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
printf("\nValues of matrix B:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++){
scanf("%d",&b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
printf("\nSummation of matrix are: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf(" %d",sum[i][j]);
if(j==c-1){
printf("\n\n");
}
}
return 0;
}
Input/Output Screenshot:
3. Counting-
Solution:
#include<stdio.h>
#include<string.h>
int main (){
char s[1000];
int i,vowels=0,consonants=0;
printf("Given String : ");
gets(s);
for(i=0;s[i];i++){
if((s[i]>=65 && s[i]<=90)||(s[i]>=97 && s[i]<=122))
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||
s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
vowels++;
else
consonants++;
}
}
printf("Vowels = %d\n",vowels);
printf("Consonants = %d",consonants);
return 0;
}
Input/Output Screenshot:
4.String Functions:
a.strlen-
The strlen() function calculates the length of a given string.The strlen() function is defined
in string.h header file. It doesn’t count null character ‘\0’.
Syntax:
int strlen(const char *str);
Parameter:
• str: It represents the string variable whose length we have to find.
Return: This function returns the length of string passed.
Example:
#include<stdio.h>
#include<string.h>
int main (){
char str[]={"Abeer"};
printf("Length of string is = %d",strlen (str));
return 0;
}
b.strcmp-
strcmp() is a built-in library function and is declared in <string.h> header file. This function
takes two strings as arguments and compare these two strings lexicographically.
Syntax:
int strcmp(const char * str1, const char * str2);
Parameters:
• str1 – a string
• str2 – a string
Return Value From strcmp()-
Example:
#include<stdio.h>
#include<string.h>
int main (){
char str1[]={"Abeer"};
char str2[]={"Abeer"};
int res = strcmp(str1,str2);
if (res==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
c.strcpy-
strcpy() is a standard library function in C and is used to copy one string to another. In C it
is present in string.h header file.
Syntax:
char*strcpy(char*dest, const char*src);
Parameters:
• dest: Pointer to the destination array where the content is to be copied.
• src: string which will be copied.
Return Value: After copying the source string to the destination string, the strcpy() function
returns a pointer to the destination string.
Example:
#include<stdio.h>
#include<string.h>
int main (){
char str1[10]={"Abeer"};
char str2[10];
strcpy(str2,str1);
puts(str2);
return 0;
}
d.strcat-
The strcat() function is used for string concatenation. It concatenates the specified string at the
end of the another specified string.
Syntex:
char*strcat(char*str1, const char*str2);
Parameters:
• dest: This is a pointer to the destination array, which should contain a C string, and
should
be large enough to contain the concatenated resulting string.
• src: . This is the string to be appended. This should not overlap the destination.
Return value: The strcat() function returns dest, the pointer to the destination string.
Example:
#include<stdio.h>
#include<string.h>
int main (){
char str1[10]={"My name "};
char str2[10]={"is Abeer"};
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}