Test2
This commit is contained in:
parent
80f074f434
commit
954104e5f0
135
wordrepl.c
Normal file
135
wordrepl.c
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
1. Einlesen einer Zeichenkette
|
||||||
|
2. Bestimmtes Wort kann durch ein anderes eretzt werden
|
||||||
|
3. Alle vorkommen des ausgewählten Wortes werden in der gesamten
|
||||||
|
Zeichenkette automatisch durch das neue Wort ersetzt
|
||||||
|
4. Ausgabe der neuen Zeichenkette
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#define MAX_LENGTH 101
|
||||||
|
#define MAX_WORD_LENGTH 11
|
||||||
|
#define MAX_EXCHANGE_WORD_LENGTH 21
|
||||||
|
#define MAX_POSITION 10
|
||||||
|
|
||||||
|
void get_input_string(char * input ) {
|
||||||
|
printf("Geben Sie einen Text mit maximal %d Zeichen ein: ", MAX_LENGTH-1);
|
||||||
|
scanf("%100[^\n]", input);
|
||||||
|
while (getchar() != '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_word_to_replace(char*get_word) {
|
||||||
|
printf("Welches Wort soll ersetzt werden? (Maximale Wortlänge: %d): ", MAX_WORD_LENGTH-1);
|
||||||
|
scanf("%10[^\n]", get_word);
|
||||||
|
while (getchar() != '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_new_word(char*new_word) {
|
||||||
|
printf("Durch welches Wort soll dieses ersetzt werden? (Maximale Wortlänge des neuen Wortes: %d): ", MAX_EXCHANGE_WORD_LENGTH-1);
|
||||||
|
scanf("%20[^\n]", new_word);
|
||||||
|
while (getchar() != '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
int find_word_positions(const char *input_sentence, const char *target_word, int position_target_word[]) {
|
||||||
|
|
||||||
|
int counter_word_found = 0;
|
||||||
|
|
||||||
|
char *sentence_ptr = (char*)input_sentence;
|
||||||
|
|
||||||
|
if(strstr(sentence_ptr, target_word) == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
do{
|
||||||
|
char *ptr_position_target_word = strstr(sentence_ptr, target_word);
|
||||||
|
|
||||||
|
int position_before_target_word = ptr_position_target_word - input_sentence - 1;
|
||||||
|
|
||||||
|
int position_after_target_word = ptr_position_target_word - input_sentence + strlen(target_word);
|
||||||
|
|
||||||
|
if (isalpha(input_sentence[position_before_target_word]) || isalpha(input_sentence[position_after_target_word])) {
|
||||||
|
sentence_ptr = ptr_position_target_word + strlen(target_word);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
position_target_word[counter_word_found] = ptr_position_target_word - input_sentence;
|
||||||
|
|
||||||
|
sentence_ptr = ptr_position_target_word + strlen(target_word);
|
||||||
|
|
||||||
|
counter_word_found++;
|
||||||
|
}
|
||||||
|
}while(strstr(sentence_ptr, target_word) != NULL);
|
||||||
|
|
||||||
|
return counter_word_found;
|
||||||
|
}
|
||||||
|
|
||||||
|
void replace_word_in_sentence(char *input_sentence, const char *replacement_word, const char *target_word, int positions_target_word[], int counter_word_found) {
|
||||||
|
|
||||||
|
char updated_sentence[MAX_LENGTH] = {0};
|
||||||
|
|
||||||
|
int last_position = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < counter_word_found; i++) {
|
||||||
|
|
||||||
|
strncat(updated_sentence, input_sentence + last_position, positions_target_word[i] - last_position);
|
||||||
|
|
||||||
|
strncat(updated_sentence, replacement_word, MAX_EXCHANGE_WORD_LENGTH - 1);
|
||||||
|
|
||||||
|
last_position = positions_target_word[i] + strlen(target_word);
|
||||||
|
}
|
||||||
|
strncat(updated_sentence, input_sentence + last_position, MAX_LENGTH - strlen(updated_sentence) - 1);
|
||||||
|
|
||||||
|
strncpy(input_sentence, updated_sentence, MAX_LENGTH - 1);
|
||||||
|
input_sentence[MAX_LENGTH] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
char input_sentence[MAX_LENGTH] = {0};
|
||||||
|
char target_word[MAX_WORD_LENGTH] = {0};
|
||||||
|
char replacement_word[MAX_EXCHANGE_WORD_LENGTH] = {0};
|
||||||
|
int positions_target_word[MAX_POSITION] = {0};
|
||||||
|
char continue_choice;
|
||||||
|
int word_found = 0;
|
||||||
|
|
||||||
|
|
||||||
|
get_input_string(input_sentence);
|
||||||
|
|
||||||
|
do {
|
||||||
|
get_word_to_replace(target_word);
|
||||||
|
|
||||||
|
get_new_word(replacement_word);
|
||||||
|
|
||||||
|
word_found = find_word_positions(input_sentence, target_word, positions_target_word);
|
||||||
|
|
||||||
|
if (word_found == 0) {
|
||||||
|
printf("Wort nicht gefunden:\n");
|
||||||
|
printf("Der Satz lautet: %s\n", input_sentence);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
replace_word_in_sentence(input_sentence, replacement_word, target_word, positions_target_word, word_found);
|
||||||
|
printf("\n....: %s\n", input_sentence);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Noch eine Einsetzung (j/n)? ");
|
||||||
|
continue_choice = getchar();
|
||||||
|
while (getchar() != '\n');
|
||||||
|
|
||||||
|
} while (continue_choice != 'n' && continue_choice != 'N');
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Created by Lennart Pecher on 25.04.25.
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Created by Lennart Pecher on 25.04.25.
|
||||||
|
//
|
Loading…
x
Reference in New Issue
Block a user