Chap01 Array String Pointer en
Chap01 Array String Pointer en
Basis
content
• Recommended environment:
• Operating system: UNIX
• Compiler: gcc
• Source code editor: Emacs, K-Developer.
Introduction to the course
… 0 1 2 3 4 5 6 7 8 9 …
• Example 1.
#include <stdio.h>
• Write a program that takes input of an array
of 10 integer numbers from the keyboard int main(void)
and prints the entered values in reverse {
order. int i, A[10];
return 0;
}
• Example 2. Write a program that inputs a string containing only lowercase letters.
Print the frequency of each alphabetical character in the string, ignoring non-alphabetic characters.
The output for the input line: “hello, world!”
int main(void)
{
int i = 0, count[ALPHABET_LEN] = { 0 }; // initialize all its elements to zero
char c = '\0';
int main() {
int i = 0, count[ALPHABET_LEN] = { 0 }; // initialize all its elements to zero
char s[20], c = '\0';
printf("Please enter a line of text: \n");
gets(s);
for (i = 0; i < strlen(s); i++) {
c = s[i];
if (c <= 'z' && c >= 'a') {
++count[c - 'a'];
}
}
for (i = 0; i < ALPHABET_LEN; ++i) {
if (count[i] > 0) {
printf("The letter '%c' appears %d time(s).\n", 'a' + i, count[i]);
}
}
return 0;
}
Array
#define SIZE 5
/*function check if two arays are identical*/
int compare_arrays(int arr1[], int arr2[], int size)
{
int i = 0;
return 0;
}
Lab test
Input Output
5 2
32479
• Lab 02. Sum Array
• Given a sequence of integers a1, a2, ..., an. Compute the sum Q of elements of this sequence.
• Input
• Line 1: contains n (1 <= n <= 10000)
• Line 2: contains a1, a2, ..., an (-10000 <= ai <= 10000)
• Output
• Write the value of Q
Input Output
4 14
3254
String
Strings
• An array of characters
• Used to store text
• Another way to initialize:
char str[] = “Hello world";
…. 's'
'H' '#'
'e' ''l'' 'f'
'l' 'd'
'o' 'y'
'' '4'
'w' '7'
'o' '$'
'r' '_'
'l' 'e'
'd' 'g'
'\0' 'd' '.' 'p' 'v' ….
str
Terminator
• getchar()
• c = getchar() strlen(const char s[])
• scanf returns the length of s
• scanf("%s", str); strcmp(const char s1[], const char
s2[])
• gets() compares s1 with s2
• gets(str); strcpy(char s1[], const char s2[])
copies to contents of s2 to s1
Example
int main(void)
{
char str[STRING_LEN + 1];
char replace_what, replace_with, tmp;
• Exercise 1. Write a program that reads a string representing a sentence from the user. Then the
program displays each word in the sentence on a separate line. A word is a sequence of consecutive
characters without containing any whitespace.
• Example:
• Input: "The house nextdoor is very old."
• Result:
• The
• house
• ...
Exercise
• Exercise 2. Write a program that asks the user to enter the number of students in a class, then input
the full names of each student in Vietnamese. Display the list of students sorted by their names. For
example:
• Nguyen Bao Anh
• Tran Quang Binh
• Vuong Quoc Binh
• Dao Thi Ha
• Ngo Anh Vu
• Advanced (optional): Display the maximum count of students with the same name.
Exercise
Input Output
Hanoi University Of Science and Technology 12
School of Information and Communication Technology
Exercise
type *variable_name;
C
… 7 3 4 …
172 173 174 175 176 177 178 179 180 181
Ptr
… 174 3 4 …
832 833 834 835 836 837 838 839 840 841
Referencing and Dereferencing
int n;
int* iptr; /* khai báo P là một con trỏ kiểu int */
n = 7;
iptr = &n;
Example 1. Write a function that accepts a double parameter and returns its integer and fraction
parts.
Write a program that accepts a number from the user and prints out its integer and fraction parts,
using this function.
int main(void)
{
double num, fraction;
int integer;
return 0;
}
Example
• Exercise 1. Write a program capable of generating random sentences using the selection
technique based on random numbers.
• The program uses four arrays of strings to store articles, nouns, verbs, and prepositions.
• Sentences are created by randomly selecting elements from these arrays and
concatenating them in the following order: article, noun, verb, article, and noun.
• The generated sentence needs to start with an uppercase letter and end with a period.
The program needs to generate a minimum of 10 sentences.
• Example of array elements:
• Articles: "the", "a", "one", "some", and "any";
• Nouns: "boy", "girl", "dog", "town", and "car";
• Verbs: "drove", "jumped", "ran", "walked", and "skipped";
• Prepositions: "to", "from", "over", "under", and "on".
Command-line arguments
Command line arguments
argc : 3
argv :
p t 1
r e 7
o x 8
g t \0
n \0
a
m
e
\0
Example
• Example 1. Write a program that accepts two numbers as command line arguments, representing a
rectangle’s height and width (as floating-point numbers).
• The program should display the rectangle’s area and perimeter
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
double width, height;
if (argc != 3){
printf("Wrong number of arguments!\n");
printf("CORRECT SYNTAX : RECT <WIDTH> <HEIGHT>\n");
return 1;
}
width = atof(argv[1]);
height = atof(argv[2]);
printf("The rectangle's area is %f\n", width* height);
printf("The rectangle's perimeter is %f\n", 2 * (width + height));
return 0;
}
Exercise
• Exercise 2. Write a program named "sde" that accepts command-line arguments as the
coefficients of a quadratic equation ax^2 + bx + c = 0 and solves the equation, then
displays the roots on the screen.
• Syntax using sde a b c
• For example: ./sde 1 2 1 yields the result: x1 = x2 = -1