Quantcast
Channel: Tópicos
Viewing all articles
Browse latest Browse all 11336

Não consigo encontrar bug em código C

$
0
0
#include <stdio.h> #include <string.h> #define BUFFER_SIZE 256 #define RemoveLineFeed(str, len) if ((str)[(len) - 1] == '\n') (str)[--(len)] = '\0' int main(void) { // variaveis char haystack[BUFFER_SIZE], needle[BUFFER_SIZE], substitute[BUFFER_SIZE], * check; size_t haystack_size, needle_size, substitute_size; // leitura das strings do { printf("Digite o texto original : "); fflush(stdout); } while (! fgets(haystack, BUFFER_SIZE, stdin) || ! haystack[0] || haystack[0] == '\n'); haystack_size = strlen(haystack); do { printf("Digite o texto a ser pesquisado/alterado : "); fflush(stdout); } while (! fgets(needle, BUFFER_SIZE, stdin) || ! needle[0] || needle[0] == '\n'); needle_size = strlen(needle); do { printf("Digite o texto ser substituido : "); fflush(stdout); } while (! fgets(substitute, BUFFER_SIZE, stdin) || ! substitute[0] || substitute[0] == '\n'); substitute_size = strlen(substitute); RemoveLineFeed(haystack, haystack_size); RemoveLineFeed(needle, needle_size); RemoveLineFeed(substitute, substitute_size); // magia em meia duzia de linhas while ((check = strstr(haystack, needle)) != NULL) { void * remainder = (void *) ((size_t) check + needle_size); size_t shift = substitute_size - needle_size; memmove(remainder + shift, remainder, (size_t) haystack + haystack_size + 1 - (size_t) remainder); memcpy(check, substitute, substitute_size); haystack_size += shift; } printf("O texto final : %s\n", haystack); return 0; }

Viewing all articles
Browse latest Browse all 11336

Trending Articles