Строки (C): различия между версиями
Перейти к навигации
Перейти к поиску
Строка 3: | Строка 3: | ||
* [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> | ||
== Задачник == | == Задачник == |
Версия 13:48, 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 int code = 55;
9 printf("Char %c has code %d\n", code, code);
10
11 char* str = "Hello";
12 printf("String length %s = %d\n", str, strlen(str));
13
14 bool hasl;
15 if (strchr(str, 'l') != NULL) {
16 hasl = true;
17 } else {
18 hasl = false;
19 }
20 printf("String %s has letter l = %s\n", str, hasl? "true" : "false");
21
22 bool hasz;
23 if (strchr(str, 'z') != NULL) {
24 hasz = true;
25 } else {
26 hasz = false;
27 }
28 printf("String %s has letter z = %s\n", str, hasz? "true" : "false");
29 }
Задачник
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 }