Professional Documents
Culture Documents
EE171 Lecture 6 Structured-Data Types
EE171 Lecture 6 Structured-Data Types
1
Arrays
Strings
Structures
Unions
2
Arrays
3
# include <stdio.h>
int main( ) {
char firstname[30];
printf(“What is your first name: ”);
scanf(“%s”, firstname);
printf(“Your first name is %s \n”, firstname);
}
Strings Input/Output
19
# include <stdio.h>
int main( ) {
char firstname[30];
printf(“What is your first name: ”);
gets(firstname);
printf(“Your first name is %s \n”, firstname);
}
Strings Input/Output
20
# include <stdio.h>
int main( ) {
char firstname[30];
printf(“What is your first name: ”);
fgets(firstname, 30, stdin);
printf(“Your first name is %s \n”, firstname);
}
Strings Input/Output…
21
# include <stdio.h>
int main( ) {
char firstname[10], surname[10];
printf(“What is your first name: ”);
scanf(“%s”, firstname);
printf(“What is your surname name: ”);
scanf(“%s”, surname);
printf(“Your full name is %s %s\n”, firstname, surname);
}
String Handling Functions
22
Function Purpose
strlen() Returns the length of the string
strlwr() Converts a string to lowercase
strupr() Converts a string to uppercase
strrev() Shows a string in reverse order
strcpy(str1, str2) Copies string str2 into string str1
strcmp(str1, str2) Compares two strings
strcat(str1, str2) Concatenates (combines) two strings
Strings Handling Functions…
24
# include <stdio.h>
# include <string.h>
int main( ) {
char name[6] = “Deus”;
printf(“%d \n”, strlen(name));
printf(“ %s \n”, strlwr(name));
printf(“ %s \n”, strupr(name));
printf(“ %s \n”, strrev(name));
}
Strings Handling Functions…
25
# include <stdio.h>
# include <string.h>
int main( ) {
char firstname[6] = “Deus”;
char surname[6] = “Msaki”;
# include <stdio.h>
# include <string.h>
int main( ) {
char firstname[20] = “Dominick”;
char surname[20];
strcpy(surname, firstname);
printf(“My surname name is %s \n”, surname);
}
Strings Handling Functions…
27
# include <stdio.h>
# include <string.h>
int main( ) {
char firstperson[10] = “Deus”;
char secondperson[10] = “Zacharia”;
int c = strcmp(firstperson, secondperson);
if (c == 0)
printf(“The two names are identical\n”);
else
printf(“The two names are NOT identical \n”);
}
Arrays
Strings
Structures
Unions
28
Structures
29
37
#include <stdio.h>
struct Student {
int regno;
float marks;
char grade;
};
int main(){
struct Student omary;
omary.regno = 2020;
omary.marks = 68;
omary.grade = 'B';
38 }
Structure Initialization
39
#include <stdio.h>
struct Student {
int regno;
float marks;
};
int main(){
struct Student omary;
omary.regno = 2020;
omary.marks = 68;
printf("The student reg. number is: %d \n", omary.regno);
printf("The student marks is: %f \n", omary.marks);
}
Structures
Unions
Functions
42
Exercise 2
43