Home > Blockchain >  Reversing a substring inside a string
Reversing a substring inside a string

Time:12-12

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>. 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.

CodePudding user response:

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;
    for (int i = 0; sentence[i] != '\0'; i  )  // Stop at the end of the sentence
    {
        // If both sentence[i] and word[counter] == '\0' so this is 
        // should return because of this have the word[counter] != '\0'

        if (sentence[i] == word[counter] && word[counter] != '\0')
        {
            counter  ; // Iterate over word
        }
        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
        }
    }
    return -1; // Not found
}

int main()
{
    char s[] = "This is the input sentence";
    char change_what[] = "input";
    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;
} 

CodePudding user response:

Your code is much too complicated:

  • you should functions to:

    • compute the length of a string
    • find a substring inside another string
    • reverse a substring
  • then in the main function, you should read a sentence and a word, perform the replacement and output the modified sentence.

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, in len) {
    int i;
    if (len == 0)
        return 0;
    for (i = 0; s1[i] != '\0'; i  ) {
        for (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;
}
  •  Tags:  
  • c
  • Related