CSE Assignment -2
CSE Assignment -2
Assignment: 2
Course Code: CSE 111
Course: Structured Programming Language
#include<stdio.h>
struct payment{
float paid,due,waiver;
};
struct student{
int id,intake,section;
char name[36];
struct payment pay;
};
int main(){
struct student s;
s.id;
s.pay.paid;
printf("Input are:\n");
scanf("%d",&s.id);
scanf("%s",s.name);
scanf("%d",&s.intake);
scanf("%d",&s.section);
scanf("%f",&s.pay.paid);
printf("Output are:\n");
printf("Id number-%d\n",s.id);
printf("Name-%s\n",s.name);
printf("Intake-%d\n",s.intake);
printf("Section-%d\n",s.section);
printf("Paid-%.2f",s.pay.paid);
return 0;
}
Input/Output Screenshot:
2.Matrix Summation-
Solution:
#include <stdio.h>
int main()
{
int A[10][10],B[10][10],C[10][10],i,j,row,col;
printf("\n Enter the row, colum the matrix: ");
scanf("%d %d",&row,&col);
printf("\n Enter A matrix:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("\n Enter B matrix:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("\n Sum of the two matrices: \n ");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
C[i][j]=A[i][j]+B[i][j];
printf("%d \t",C[i][j]);
}
printf("\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;
}