120 lines
3.1 KiB
C
120 lines
3.1 KiB
C
|
#include <stdio.h>
|
||
|
|
||
|
int string_length(char str[]) {
|
||
|
int length = 0;
|
||
|
while (str[length] != '\0') {
|
||
|
length++;
|
||
|
}
|
||
|
return length;
|
||
|
}
|
||
|
|
||
|
void string_concatenate(char str1[], char str2[], char result[]) {
|
||
|
int i = 0, j = 0;
|
||
|
while (str1[i] != '\0') {
|
||
|
result[j++] = str1[i++];
|
||
|
}
|
||
|
i = 0;
|
||
|
while (str2[i] != '\0') {
|
||
|
result[j++] = str2[i++];
|
||
|
}
|
||
|
result[j] = '\0';
|
||
|
}
|
||
|
|
||
|
int string_compare(char str1[], char str2[]) {
|
||
|
int i = 0;
|
||
|
while (str1[i] != '\0' && str2[i] != '\0') {
|
||
|
if (str1[i] != str2[i]) {
|
||
|
return (str1[i] < str2[i]) ? -1 : 1;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
if (str1[i] == '\0' && str2[i] == '\0') {
|
||
|
return 0;
|
||
|
} else if (str1[i] == '\0') {
|
||
|
return -1;
|
||
|
} else {
|
||
|
return 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void string_insert(char str[], char substr[], int pos, char result[]) {
|
||
|
int i = 0, j = 0, k = 0;
|
||
|
int str_length = string_length(str);
|
||
|
int substr_length = string_length(substr);
|
||
|
|
||
|
while (i < pos && i < str_length) {
|
||
|
result[j++] = str[i++];
|
||
|
}
|
||
|
while (substr[k] != '\0') {
|
||
|
result[j++] = substr[k++];
|
||
|
}
|
||
|
while (i < str_length) {
|
||
|
result[j++] = str[i++];
|
||
|
}
|
||
|
result[j] = '\0';
|
||
|
}
|
||
|
|
||
|
void string_delete(char str[], int pos, int length, char result[]) {
|
||
|
int i = 0, j = 0;
|
||
|
int str_length = string_length(str);
|
||
|
|
||
|
while (i < pos && i < str_length) {
|
||
|
result[j++] = str[i++];
|
||
|
}
|
||
|
i += length;
|
||
|
while (i < str_length) {
|
||
|
result[j++] = str[i++];
|
||
|
}
|
||
|
result[j] = '\0';
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
char str1[100], str2[100], result[200];
|
||
|
int choice, pos, length;
|
||
|
|
||
|
printf("Enter the first string: ");
|
||
|
scanf("%99s", str1);
|
||
|
printf("Enter the second string: ");
|
||
|
scanf("%99s", str2);
|
||
|
|
||
|
printf("\nChoose an operation:\n");
|
||
|
printf("1. Length of the first string\n");
|
||
|
printf("2. Concatenate strings\n");
|
||
|
printf("3. Compare strings\n");
|
||
|
printf("4. Insert second string into first string at position\n");
|
||
|
printf("5. Delete substring from first string\n");
|
||
|
printf("Enter your choice: ");
|
||
|
scanf("%d", &choice);
|
||
|
|
||
|
switch (choice) {
|
||
|
case 1:
|
||
|
printf("Length of the first string: %d\n", string_length(str1));
|
||
|
break;
|
||
|
case 2:
|
||
|
string_concatenate(str1, str2, result);
|
||
|
printf("Concatenated string: %s\n", result);
|
||
|
break;
|
||
|
case 3:
|
||
|
printf("String comparison result: %d\n", string_compare(str1, str2));
|
||
|
break;
|
||
|
case 4:
|
||
|
printf("Enter position to insert the second string: ");
|
||
|
scanf("%d", &pos);
|
||
|
string_insert(str1, str2, pos, result);
|
||
|
printf("String after insertion: %s\n", result);
|
||
|
break;
|
||
|
case 5:
|
||
|
printf("Enter position to start deletion: ");
|
||
|
scanf("%d", &pos);
|
||
|
printf("Enter length of substring to delete: ");
|
||
|
scanf("%d", &length);
|
||
|
string_delete(str1, pos, length, result);
|
||
|
printf("String after deletion: %s\n", result);
|
||
|
break;
|
||
|
default:
|
||
|
printf("Invalid choice.\n");
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|