PW Test Sollution Algo
PW Test Sollution Algo
PW Test Sollution Algo
12
Pw test 12
Exercise 1 ☹(8/8)
What does the `strcat` function do in C? Complete this example with `strcat`
#include <stdio.h>
#include <string.h>
- Explanation: `strcat` function in C is used to
concatenate two strings. It appends the content of the int main() {
1
source string to the destination string. char dest[20] = "Hello ";
char src[] = "world!";
strcat(dest, src);
printf("Concatenated string: %s\n", dest);
1
return 0;
}
Explain the purpose of the `strcmp` function in Complete this example with ` strcmp`
C. #include <stdio.h>
#include <string.h>
Explanation: `strcmp` function compares two strings
int main() {
lexicographically. It returns 0 if the strings are equal, a 1
char str1[] = "hello";
negative value if the first string is lexicographically less char str2[] = "world";
than the second, and a positive value if the first string is int result = strcmp(str1, str2);
lexicographically greater than the second. if (result == 0) {
printf("Strings are equal\n");
} else {
1
printf("Strings are not equal\n");
}
return 0;
}
Describe the role of the `strlen` function in C Complete this example with ` strlen`
#include <stdio.h>
- Explanation: `strlen` function calculates the length of #include <string.h>
a string, excluding the null-terminator.
int main() {
char str[] = "Hello, world!"; 1
int length = strlen(str);
1 printf(" %d\n", length);
return 0;
}
A.Y
Faculty of Sciences
Computer Science Department Academic Year: 2023-2024
What is the purpose of the `strncpy` function? Complete this example with ` strncpy `
#include <stdio.h>
- Explanation: `strncpy` function copies a specified #include <string.h>
number of characters from the source string to the int main() {
destination string. It ensures that the destination string is char src[] = "Hello, world!";
null-terminated if the specified number of characters char dest[20]; // Destination buffer
// Copy the first 5 characters from src to dest
exceeds the length of the source string. strncpy(dest, src, 5); dest[5] = '\0';
printf("%s\n", dest);
1
1
return 0;
}
Exercise 2 (4/4)
Let T be an array containing the description of cars in a car park. A car is described by the following fields:
- Registration Number (string of size 12): 05 characters for the serial number, 01 character drawn, 01 character
for the category, and 02 characters for the year of construction, 01 character drawn, and 02 characters for the
wilaya number.
Serial number 02517, drawn, category 3, year 2016, drawn, wilaya of Médéa 26
- Price (real).
1- Propose a data structure to represent this statement (declaration of types), Next, write the main algorithm.
#include <stdio.h> // Iterate through the array and print car details
#include <string.h> for (int i = 0; i < MAX_CARS; i++) {
#define MAX_BRAND_LEN 11 // If serial number is empty, break the loop
#define MAX_COLOR_LEN 11 if (strlen(cars[i].regNumber.serialNumber) == 0)
#define MAX_CARS 100 break;
A.Y
Faculty of Sciences
Computer Science Department Academic Year: 2023-2024
A.Y