C语言 反转字符串内的子字符串

dauxcl2d  于 2022-12-22  发布在  其他
关注(0)|答案(2)|浏览(222)

我正在尝试查找、反转和替换一个句子中的单词。单词和句子可以自由更改。我们可以手动编写它们,也可以由用户编写它们。
main()中,我无法访问和获取反转函数的输出。编译器没有给予任何错误,但也没有显示任何输出。我不允许使用<stdio.h>以外的任何库。我该怎么办?

#include <stdio.h>

int stringLength(char string[100]) {
    int i, l = 0;

    for (i = 0; string[i] != '\0'; i++) {
        l++;
    }
    int length = l;
    return length;
}

char reversing(char *givenWord[]) {
    int i = 0, j = 0, k = 0;
    char temp;
    j = stringLength(*givenWord) - 1;
    while (i < j) {
        temp = *givenWord[i];
        *givenWord[i] = givenWord[j];
        *givenWord[j] = temp;
        i++;
        j--;
    }
    return *givenWord;
}

int compare(char *x, char *y) {
    while (*x != '\0' || *y != '\0') {
        if (*x == *y) {
            x++;
            y++;
        }
            // if they are not equal
        else
        if ((*x == '\0' && *y != '\0') || (*x != '\0' && *y == '\0') ||
            *x != *y) {
            return 0;
        }
    }
    return 1;
}

int lenght(char *a) {
    char *b;
    for (b = a; *a; a++)
        ;
    return a - b;
}

char *substring(char *main_string, char *substring) {
    while (*main_string != '\0') {
        char *p = main_string;
        char *q = substring;
        while (*p++ == *q++) {
            if (*p == ' ' || *p == '\0')
                if (*q == '\0') {
                    return main_string;
                }
        }
        main_string++;
    }
    return NULL;
}

void replace_string_add(char *s, char *change_what, char *into_what, int shift)
{
    char *i_pointer = into_what;
    char *c_pointer = change_what;
    char *position = substring(s, change_what);
    while (position != NULL) {
        char *end = position;
        while (*end != '\0') {
            end++;
        }
        while (end > position) {
            *(end + shift) = *end;
            end--;
        }
        while (*into_what != '\0') {
            *position++ = *into_what++;
        }
        position = substring(s, change_what);
        into_what = i_pointer;
        change_what = c_pointer;
    }
}

void replace_string_remove(char *s, char *change_what, char *into_what,
                           int shift)
{
    char *i_pointer = into_what;
    char *c_pointer = change_what;
    char *position = substring(s, change_what);
    while (position != NULL) {
        char *temp = position;
        while (*(temp + shift) != '\0') {
            *temp = *(temp + shift);
            temp++;
        }
        *temp = '\0';
        while (*into_what != '\0') {
            *position++ = *into_what++;
        }
        position = substring(s, change_what);
        into_what = i_pointer;
        change_what = c_pointer;
    }
}

void replace_string(char *s, char *change_what, char *into_what)
{
    int shift = lenght(into_what) - lenght(change_what);
    if (compare(change_what, into_what) == 0) {
        if (shift >= 0) {
            replace_string_add(s, change_what, into_what, shift);
        } else {
            replace_string_remove(s, change_what, into_what, -shift);
        }
    }
}

int main() {
    char s[] = "This is the input sentence", change_what[] = "input";
    reversing(change_what);
    char *word = reversing(change_what);
    char into_what = ("%s", word);
    replace_string(s, change_what, &into_what);
    printf("\"%s\"", s);
    return 0;
}

我尝试添加其他字符串并将它们声明为reverse函数的输出。

rxztt3cl

rxztt3cl1#

我相信这段代码解决了你的问题。函数中有注解,抱歉我的英语不好

#include <stdio.h>

void put_word(char *word, char *sentence, int index)
{
    // Replace the chars of the sentence with the chars of 
    // the word through the index

    // Two counter, one for sentence, other for word
    for (int i = index, c = 0; word[c] != '\0'; i++, c++)
    {
        sentence[i] = word[c];
    }
    return;
}

void reverse_word(char *word)
{
    int size = 0;
    for (int i = 0; word[i] != '\0'; i++) {size++;} // Size of the word
    
    char tmp;
    for (int i = 0; i < size / 2; i++) // string mirroring
    {
        tmp = word[i];
        word[i] = word[size -(i + 1)]; // +1 is for ignore \0
        word[size - (i + 1)] = tmp;
    }
    return;
}

int find_word(char *word, char *sentence) 
{
    // Return index of the start of the word in sentence
    int counter = 0;
    int start_word = 0; // !!! Checker for this is the start letter of a word
    int in_word = 0; // !!! Confirm that is in a word
    

    // Stop at end of the sentence
    // But run one more time also is in both strings the end
    for (int i = 0; sentence[i] != '\0' || word[counter] == '\0'; i++) 
    {
        // Check if is a start letter
        if (sentence[i - 1] == ' ') start_word = 1;

        // Same letter and not the end of the string and or in start or in the word
        if (sentence[i] == word[counter] && word[counter] != '\0' && (start_word == 1 || in_word == 1))
        {
            counter++; // Iterate over word
            in_word = 1;
        }
        else if (counter != 0) // Two options, or word is founded or not
        {
            if (word[counter] == '\0') // Indicates of the found point
            {
                return i - counter; // Basic math
            }
            counter = 0; // If not founded reset counter word
            in_word = 0; 
        }
        start_word = 0;
    }
    return -1; // Not found
}

int main()
{
    char s[] = "This is the input sentence";
    char change_what[] = "is";
    int index = find_word(change_what, s);
    if (index == -1)
    {
        printf("Word %s not found!\n", change_what);
        return 1;
    }

    // As these functions work with pointers, the return is not necessary.
    reverse_word(change_what);
    put_word(change_what, s, index);

    printf("\"%s\"", s);
    return 0;
}
5hcedyr0

5hcedyr02#

你的代码太复杂了:

  • 您应该使用函数来:
  • 计算字符串的长度
  • 在另一个字符串中查找子字符串
  • 反转子字符串
  • 然后在main函数中,读取一个句子和一个单词,执行替换并输出修改后的句子。

以下是一个简化版本:

#include <stdio.h>

int string_length(const char *str) {
    int len;
    for (len = 0; str[len] != '\0'; len++)
        continue;
    return len;
}

void string_reverse(char *str, int from, int len) {
    int i = from, j = from + len - 1;
    while (i < j) {
        char c = str[i];
        str[i] = str[j];
        str[j] = c;
        i++;
        j--;
    }
}

int string_find(const char *s1, const char *s2, int len) {
    int i;
    if (len == 0) {
        // special case the empty string so it is found inside the empty string
        return 0;
    }
    for (i = 0; s1[i] != '\0'; i++) {
        for (int j = 0;; j++) {
            if (j == len) {
                // found substring, return start index
                return i;
            }
            if (s1[i + j] != s2[j]) {
                break;
            }
        }
    }
    return -1;
}

int main() {
    // sentence and word could be read from the user.
    // Use immediate strings for testing
    char sentence[] = "This is the input sentence";
    char word[] = "input";

    int length = string_length(word);
    int index = string_find(sentence, word, length);
    if (index >= 0) {
        string_reverse(sentence, index, length);
    }
    printf("%s\n", sentence);
    return 0;
}

相关问题