You are on page 1of 11

11/27/23, 3:51 PM 4.

String: Attempt review

Đã bắt đầu vào Thứ hai, 27 Tháng mười một 2023, 3:14 PM
lúc
Tình trạng Đã hoàn thành
Hoàn thành vào Thứ hai, 27 Tháng mười một 2023, 3:48 PM
lúc
Thời gian thực 34 phút 16 giây
hiện
Điểm 5,00/5,00
Điểm 10,00 của 10,00 (100%)

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 1/11
11/27/23, 3:51 PM 4. String: Attempt review

Câu hỏi 1
Chính xác

Điểm 1,00 của 1,00

Input a string and check if the string is symmetric or not? If yes, print “The string is a palindrome.” If no, print “The string is not a palindrome”

Example:

Input Output

“abccba” The string is a palindrome

“ab ba” The string is a palindrome

“cacb” The string is not a palindrome

For example:

Test Input Result

aba aba The string is a palindrome

ab ab The string is not a palindrome

Answer: (penalty regime: 0 %)


10 ▼ if (str[left] != str[right]) {
11 return 0; // Not a palindrome
12 }
13 left++;
14 right--;
15 }
16
17 return 1; // Palindrome
18 }
19 ▼ int main() {
20 char input[100];
21 scanf("%s", input);
22
23 // Check if the string is a palindrome
24 ▼ if (isPalindrome(input)) {
25 printf("The string is a palindrome\n");
26 ▼ } else {
27 printf("The string is not a palindrome\n");
28 }
29
30 return 0;
31 }

Test Input Expected Got

 aba aba The string is a palindrome The string is a palindrome 

 ab ab The string is not a palindrome The string is not a palindrome 

Passed all tests! 

Question author's solution (C):


1 #include <stdio.h>
2 #include <string.h>
3
4 ▼ int main() {
5 char str[101];
6 int len = 0;
7 scanf("%s", str);

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 2/11
11/27/23, 3:51 PM 4. String: Attempt review
8 len = strlen(str);
9
10 int check = 1;
11 ▼ for (int i = 0; i < len / 2; i++) {
12 ▼ if (str[i] != str[len - 1 - i]) {
13 check = 0;
14 break;
15 }
16 }
17
18 if (check) printf("The string is a palindrome");
19 else printf("The string is not a palindrome");
20 return 0;
21 }

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 3/11
11/27/23, 3:51 PM 4. String: Attempt review

Câu hỏi 2
Chính xác

Điểm 1,00 của 1,00

Given a program with 3 input strings. Compare and print 3 strings in alphabetical order.

Hint: Using strcmp

For example:

Test Input Result

1 aaac aaaa
aaaa aaab
aaab aaac

2 b a c a
b
c

Answer: (penalty regime: 0 %)

Reset answer

1 #include <stdio.h>
2 #include <string.h>
3
4 // Function to compare and sort two strings
5 ▼ void sortTwoStrings(char *str1, char *str2) {
6 ▼ if (strcmp(str1, str2) > 0) {
7 char temp[100];
8 strcpy(temp, str1);
9 strcpy(str1, str2);
10 strcpy(str2, temp);
11 }
12 }
13
14 // Function to compare and sort three strings
15 ▼ void sortStrings(char *str1, char *str2, char *str3) {
16 sortTwoStrings(str1, str2);
17 sortTwoStrings(str1, str3);
18 sortTwoStrings(str2, str3);
19 }
20
21 ▼ int main() {
22 char str1[100], str2[100], str3[100];

Test Input Expected Got

 1 aaac aaaa aaaa 


aaaa aaab aaab
aaab aaac aaac

Passed all tests! 

Question author's solution (C):


1 #include <stdio.h>
2 #include <string.h>
3
4 ▼ int main() {
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 4/11
11/27/23, 3:51 PM 4. String: Attempt review
4 ▼ int main() {
5 char str1[101];
6 char str2[101];
7 char str3[101];
8 scanf("%s%s%s", str1, str2, str3);
9
10 ▼ if (strcmp(str1, str2) >= 0) {
11 ▼ if (strcmp(str2, str3) >= 0) {
12 printf("%s\n%s\n%s", str3, str2, str1);
13 }
14 ▼ else if (strcmp(str1, str3) >= 0) {
15 printf("%s\n%s\n%s", str2, str3, str1);
16 }
17 else printf("%s\n%s\n%s", str2, str1, str3);
18 }
19 ▼ else {
20 ▼ if (strcmp(str1, str3) >= 0) {
21 printf("%s\n%s\n%s", str3, str1, str2);
22 }

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 5/11
11/27/23, 3:51 PM 4. String: Attempt review

Câu hỏi 3
Chính xác

Điểm 1,00 của 1,00

Reverse the characters of a string. For example:

“ab ef” -> “fe ba”

“abc” -> “cba”

For example:

Test Input Result

asdbh asdbh hbdsa

Answer: (penalty regime: 0 %)


1 #include <stdio.h>
2 #include <string.h>
3
4 // Function to reverse the characters of a string
5 ▼ void reverseString(char str[]) {
6 int length = strlen(str);
7 int start = 0;
8 int end = length - 1;
9
10 ▼ while (start < end) {
11 // Swap characters at the start and end positions
12 char temp = str[start];
13 str[start] = str[end];
14 str[end] = temp;
15
16 // Move to the next pair of characters
17 start++;
18 end--;
19 }
20 }
21
22 ▼ int main() {

Test Input Expected Got

 asdbh asdbh hbdsa hbdsa 

Passed all tests! 

Question author's solution (C):


1 #include <stdio.h>
2 #include <string.h>
3
4 ▼ int main() {
5 char str[101];
6 int len = 0;
7 scanf("%s", str);
8 len = strlen(str);
9
10 char temp = 1;
11 ▼ for (int i = 0; i < len / 2; i++) {
12 temp = str[i];
13 str[i] = str[len - 1 - i];
14 str[len - 1 - i] = temp;
15 }
16

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 6/11
11/27/23, 3:51 PM 4. String: Attempt review
17 printf("%s", str);
18 return 0;
19 }

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 7/11
11/27/23, 3:51 PM 4. String: Attempt review

Câu hỏi 4
Chính xác

Điểm 1,00 của 1,00

Given 2 strings (only lowercase letters, guaranteed testcase). The characters in the string have been sorted in ascending order following the
ASCII table. Create a new string, in which the characters are also sorted in ascending order from the two strings above.

Input Output

“ab” “abbde”

“bde”

“bde” “abcddeef”

“acdef"

For example:

Test Input Result

acd bef acd bef abcdef

Answer: (penalty regime: 0 %)


1 #include <stdio.h>
2 #include <string.h>
3
4 // Function to merge and sort two strings
5 ▼ void mergeAndSortStrings(char str1[], char str2[], char result[]) {
6 int i = 0, j = 0, k = 0;
7
8 // Merge and sort the strings
9 ▼ while (str1[i] != '\0' && str2[j] != '\0') {
10 ▼ if (str1[i] < str2[j]) {
11 result[k++] = str1[i++];
12 ▼ } else {
13 result[k++] = str2[j++];
14 }
15 }
16
17 // Copy the remaining characters from str1, if any
18 ▼ while (str1[i] != '\0') {
19 result[k++] = str1[i++];
20 }
21
22 // Copy the remaining characters from str2, if any

Test Input Expected Got

 acd bef acd bef abcdef abcdef 

Passed all tests! 

Question author's solution (C):


1 #include <stdio.h>
2 #include <string.h>
3
4 ▼ int main() {
5 char str1[101];
6 char str2[101];
7 char str3[201];
8 scanf("%s%s", str1, str2);
9 int len1 = strlen(str1);
10 int len2 = strlen(str2);
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 8/11
11/27/23, 3:51 PM 4. String: Attempt review
10 int len2 strlen(str2);
11
12 int index1 = 0;
13 int index2 = 0;
14 int index3 = 0;
15 ▼ while (index1 < len1 && index2 < len2) {
16 ▼ if (str1[index1] <= str2[index2]) {
17 str3[index3] = str1[index1];
18 index1 += 1;
19 }
20 ▼ else {
21 str3[index3] = str2[index2];
22 index2 += 1;

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 9/11
11/27/23, 3:51 PM 4. String: Attempt review

Câu hỏi 5
Chính xác

Điểm 1,00 của 1,00

Given one string. Print it and find how many vowels and consonants are there in the string according to the format

“n m”

In which, n: number of vowels, and m: number of consonants

Vowels: a, e, o, i, u, A, E, O, I, U

Input Output

“abca bc” 24

“1a” 10

“1 23” 00

For example:

Test Input Result

abcabc abcabc 2 4

Answer: (penalty regime: 0 %)


1 #include <stdio.h>
2 #include <ctype.h>
3
4 // Function to check if a character is a vowel
5 ▼ int isVowel(char ch) {
6 ch = tolower(ch); // Convert to lowercase for case-insensitivity
7 return (ch == 'a' || ch == 'e' || ch == 'o' || ch == 'i' || ch == 'u');
8 }
9
10 // Function to count vowels and consonants in a string
11 ▼ void countVowelsConsonants(char str[], int *vowels, int *consonants) {
12 *vowels = 0;
13 *consonants = 0;
14
15 ▼ for (int i = 0; str[i] != '\0'; i++) {
16 ▼ if (isalpha(str[i])) { // Check if the character is an alphabet letter
17 ▼ if (isVowel(str[i])) {
18 (*vowels)++;
19 ▼ } else {
20 (*consonants)++;
21 }
22 }

Test Input Expected Got

 abcabc abcabc 2 4 2 4 

Passed all tests! 

Question author's solution (C):


1 #include <stdio.h>
2 #include <string.h>
3
4 ▼ int main() {
5 char str[101];
6 int len = 0;
https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 10/11
11/27/23, 3:51 PM 4. String: Attempt review
7 scanf("%s", str);
8 len = strlen(str);
9
10 int m = 0, n = 0;
11 ▼ for (int i = 0; i < len; i++) {
12 ▼ if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
13 if (str[i] == 'u' || str[i] == 'U' || str[i] == 'e' || str[i] == 'E' || str[i] == 'o' ||
14 str[i] == 'O' || str[i] == 'a' || str[i] == 'A' || str[i] == 'i' || str[i] == 'I') m += 1;
15 else n += 1;
16 }
17 }
18 printf("%d %d", m, n);
19 }

Chính xác
Điểm cho bài nộp này: 1,00/1,00.

BÁCH KHOA E-LEARNING

WEBSITE

HCMUT
MyBK
BKSI

LIÊN HỆ

 268 Lý Thường Kiệt, P.14, Q.10, TP.HCM

 (028) 38 651 670 - (028) 38 647 256 (Ext: 5258, 5234)

 elearning@hcmut.edu.vn

Copyright 2007-2022 BKEL - Phát triển dựa trên Moodle

https://e-learning.hcmut.edu.vn/mod/quiz/review.php?attempt=1733273&cmid=212130 11/11

You might also like