Строки (C): различия между версиями
Перейти к навигации
Перейти к поиску
(→Строки) |
|||
(не показано 6 промежуточных версий этого же участника) | |||
Строка 1: | Строка 1: | ||
== Строки == | == Строки == | ||
+ | * [https://docs.microsoft.com/ru-ru/cpp/c-runtime-library/string-manipulation-crt?view=msvc-160 Управление строками (CRT)] | ||
* [https://docs.microsoft.com/ru-ru/cpp/c-language/c-string-literals?view=msvc-160 Строковые литералы в C (справка Микрософт)] | * [https://docs.microsoft.com/ru-ru/cpp/c-language/c-string-literals?view=msvc-160 Строковые литералы в C (справка Микрософт)] | ||
* [https://www.tutorialspoint.com/cprogramming/c_strings.htm Строки (tutorialspoint)] | * [https://www.tutorialspoint.com/cprogramming/c_strings.htm Строки (tutorialspoint)] | ||
+ | |||
+ | <syntaxhighlight lang="c" line> | ||
+ | #include <stdio.h> | ||
+ | #include <stdbool.h> | ||
+ | #include <string.h> | ||
+ | |||
+ | int main() { | ||
+ | char ch = '0'; | ||
+ | printf("Char %c has code %d\n", ch, ch); | ||
+ | |||
+ | int code = 55; | ||
+ | printf("Char %c has code %d\n", code, code); | ||
+ | |||
+ | char* str = "Hello"; | ||
+ | printf("String length %s = %d\n", str, strlen(str)); | ||
+ | |||
+ | bool hasl; | ||
+ | if (strchr(str, 'l') != NULL) { | ||
+ | hasl = true; | ||
+ | } else { | ||
+ | hasl = false; | ||
+ | } | ||
+ | printf("String %s has letter l = %s\n", str, hasl? "true" : "false"); | ||
+ | |||
+ | bool hasz; | ||
+ | if (strchr(str, 'z') != NULL) { | ||
+ | hasz = true; | ||
+ | } else { | ||
+ | hasz = false; | ||
+ | } | ||
+ | printf("String %s has letter z = %s\n", str, hasz? "true" : "false"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Задачник == | ||
+ | * [http://ptaskbook.com/ru/tasks/string.php Строки] | ||
+ | |||
+ | === 24 === | ||
+ | <syntaxhighlight lang="c" line> | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | |||
+ | int main() { | ||
+ | char* binStr = "101001"; | ||
+ | // char binStr[20]; | ||
+ | // scanf("%s", &binStr); | ||
+ | |||
+ | int multiplier = 1; | ||
+ | int dec = 0; | ||
+ | |||
+ | for (int i = strlen(binStr)-1; i >= 0; i--) { | ||
+ | if (binStr[i] == '1') { | ||
+ | dec += multiplier; | ||
+ | } | ||
+ | multiplier *= 2; | ||
+ | } | ||
+ | |||
+ | printf("%s\n%d", binStr, dec); | ||
+ | } | ||
+ | </syntaxhighlight> |
Текущая версия на 13:55, 11 июня 2021
Строки
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <string.h>
4
5 int main() {
6 char ch = '0';
7 printf("Char %c has code %d\n", ch, ch);
8
9 int code = 55;
10 printf("Char %c has code %d\n", code, code);
11
12 char* str = "Hello";
13 printf("String length %s = %d\n", str, strlen(str));
14
15 bool hasl;
16 if (strchr(str, 'l') != NULL) {
17 hasl = true;
18 } else {
19 hasl = false;
20 }
21 printf("String %s has letter l = %s\n", str, hasl? "true" : "false");
22
23 bool hasz;
24 if (strchr(str, 'z') != NULL) {
25 hasz = true;
26 } else {
27 hasz = false;
28 }
29 printf("String %s has letter z = %s\n", str, hasz? "true" : "false");
30 }
Задачник
24
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char* binStr = "101001";
6 // char binStr[20];
7 // scanf("%s", &binStr);
8
9 int multiplier = 1;
10 int dec = 0;
11
12 for (int i = strlen(binStr)-1; i >= 0; i--) {
13 if (binStr[i] == '1') {
14 dec += multiplier;
15 }
16 multiplier *= 2;
17 }
18
19 printf("%s\n%d", binStr, dec);
20 }