简体   繁体   English

反转字符串中的 substring

[英]Reversing a substring inside a string

I'm trying to find, reverse, and replace a word inside a sentence.我正在尝试查找、反转和替换句子中的单词。 Word and sentence are free to change.单词和句子可以自由更改。 We can write them manually or the user can write them.我们可以手动编写它们,也可以由用户编写。

In main() I cannot access and take the output of the reversing function. Compiler does not give any error but it also does not show any output. I am not allowed to use any library other than <stdio.h> .main()中,我无法访问和获取反向 function 的 output。编译器没有给出任何错误,但它也没有显示任何 output。我不允许使用<stdio.h>以外的任何库。 What should I do?我应该怎么办?

#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;
}

I tried to add other strings and declare them as the reverse function's output.我尝试添加其他字符串并将它们声明为反向函数的 output。

I believe this code solves your problem.我相信这段代码可以解决您的问题。 There are comments in the functions and sorry for my bad english函数中有注释,抱歉我的英语不好

#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;
} 

Your code is much too complicated:您的代码太复杂了:

  • you should use functions to:你应该使用函数来:

    • compute the length of a string计算字符串的长度
    • find a substring inside another string在另一个字符串中找到 substring
    • reverse a substring反转 substring
  • then in the main function, you should read a sentence and a word, perform the replacement and output the modified sentence.然后在main function 中,你应该读一个句子和一个单词,执行替换和 output 修改的句子。

Here is a simplified version:这是一个简化版本:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM