Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

StringOperations

.cpp
Скачиваний:
0
Добавлен:
19.06.2023
Размер:
1.83 Кб
Скачать
#include <iostream>
#include <fcntl.h>
#include <io.h>

using namespace std;

wchar_t str1[82], str2[82];

// Добавляет строку source к строке destination
void strcat(wchar_t* destination, wchar_t* source) {
	int i = 0;
	while (destination[i] != '\0')
		++i;
	int j = 0;
	while (source[j] != '\0')
		destination[i++] = source[j++];
	destination[i] = '\0';
}

// Копирует строку source в строку destination
void strcpy(wchar_t* destination, wchar_t* source) {
	int i = 0;
	while (source[i] != '\0') {
		destination[i] = source[i];
		++i;
	}
	destination[i] = '\0';
}

// Возвращает длину строки
int strlen(wchar_t* str) {
	int i = 0;
	while (str[i++]);
	return i - 1;
}

/* Сравнивает строки.
Возвращаемое значение:
	-1, если str1 < str2
	1,  если str1 > str2
	0,  если str1 = str2
*/
int strcmp(wchar_t* str1, wchar_t* str2) {
	int i = 0;
	while (str1[i] != '\0' and str2[i] != '\0' and str1[i] == str2[i])
		++i;
	if (str1[i] < str2[i])
		return -1;
	if (str1[i] > str2[i])
		return 1;
	return 0;
}

int main()
{
	(void)_setmode(_fileno(stdout), _O_U16TEXT);
	while (true) {
		wcout << L"\nВведите строку 1:\n";
		wscanf_s(L"%40s", str1, _countof(str1));
		wcout << L"\nВведите строку 2:\n";
		wscanf_s(L"%40s", str2, _countof(str2));

		wprintf(L"\nДлина строки 1:\t\t\t %d\nДлина строки 2:\t\t\t %d\nСравнение {str1 ? str2}:\t %d", strlen(str1), strlen(str2), strcmp(str1, str2));
		strcat(str1, str2);
		wprintf(L"\nСцепление строк:\t\t %s", str1);
		strcpy(str1, str2);
		wprintf(L"\nКопирование строки 2:\t\t %s\n\n-----------------------\n", str1);
	}
}
Соседние файлы в предмете Программирование