Лекция 03 - Стрингове
Решения на задачите за стрингове
#include <string.h>
#include <stdio.h>
int strcmp2(char[], char[]);
int strlen2(char[]);
int main() {
char str[6] = {'h', 'i', '\0', 'h', 'i', '\0'};
printf("%s\n", str); // hi
printf("%zu - %d\n", strlen(str), strlen2(str)); // 2
printf("%d - %d\n", strcmp("asd", "xyz"), strcmp2("asd", "xyz"));
printf("%c - %d\n", 'a', 'a');
printf("%c - %d\n", 'x', 'x');
printf("%d\n", 'a' - 'x');
for (int i = 'A'; i <= 'Z'; i++) {
printf("%c", i);
}
printf("\n");
for (int i = 65; i <= 90; i++) {
printf("%c", i);
}
return 0;
}
int strcmp2(char str1[], char str2[]) {
for (int i = 0; i < strlen2(str1); i++) {
/* v1
if (str1[i] < str2[i]) {
return -1;
}
if (str1[i] > str2[i]) {
return 1;
}*/
if (str1[i] != str2[i]) {
return str1[i] - str2[i];
}
}
return 0;
}
int strlen2(char str[]) {
/* v1
int i = 0;
while(str[i] != '\0') {
i++;
}*/
int i;
for (i = 0; str[i] != '\0'; i++) {}
return i;
}
#include <stdio.h>
#include <string.h>
int main() {
int arr1[] = {1, 2, 3};
char arr2[] = {'a', 'b', 'c'};
int arr3[] = {'a', 'b', 'c'};
char arr4[] = {97, 98, 99};
char arr5[] = { "abc" };
printf("%d %c\n", arr1[0], arr1[0]);
printf("%d %c\n", arr2[1], arr2[1]);
printf("%d %c\n", arr3[2], arr3[2]);
printf("%d %c\n", arr2[2] - arr2[1], arr2[2] - arr2[1]);
printf("%s\n", arr5);
char arr6[] = {'a', 'b', 'c', '\0', 'd','e'};
printf("%d %s\n", (int)strlen(arr6), arr6);
char str1[10] = "abc";
char str2[] = "def";
strcat(str1, str2);
char str3[] = {strcat(str1, str2)};
printf("%s\n", str1);
char str4[] = "abc";
char str5[] = "abc";
char str6[] = "bcd";
char str7[] = "2ab";
printf("%d\n", strcmp(str4, str5));
printf("%d\n", strcmp(str4, str6));
printf("%d\n", strcmp(str4, str7));
strcpy(str4, str7);
printf("%d\n", strcmp(str4, str7));
return 0;
}