AlexL AlexL - 1 month ago 7
C Question

Changing word in string with another word in C

I am looking for a way to change words in string.
I want to change word to another word which have more symbols than first word.
First I count the symbols of the word which i want to change with another one,
than I count how many this word is in the text which is in my program as a char array(string) and now i want to change this word with the word which has more symbols.
There is the code:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
Start:
printf("This program can change word or symbols combanation in text\n\n");
char text[550] = "There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\nFood was in plenty for them. hey also wasted the bread\nbiscuits and fruits of the shop.\nThe grocer got really worried. So, he thought I should buy a cat\nand let it stay at the grocery. Only then I can save my things.\nHe bought a nice, big fat cat and let him stay there. The cat had a nice\nime hunting the mice and killing them.\nThe mice could not move freely now. They were afraid that\n\n";
printf("%s\n", text);
int symbols_in_text = 0;
for (int f = 0;f < 550;f++)
{
if (text[f])
{
symbols_in_text++;
}
else
{
break;
}
}
printf("Text has %i symbols\n\n", symbols_in_text);
char b[15];
printf("Which word would you like to chenge in text above?\n(Maximum quantity of Symbols is 15)\nType word or some symbol(s)\n--->>");
cin >> b;
int word_symbols=0;
for (int a = 0;a < 15;a++)
{
if (b[a])
{
word_symbols++;
}
else
{
break;
}
}
printf("Word which you have entered has %i symbols\n", word_symbols);
int word_in_text = 0;
for (int i = 0; i < 550 - word_symbols; i++)
{
int found = 1;
for (int j = 0; j < word_symbols; j++)
{
if (b[j] != text[i + j])
{
found = 0;
break;
}
}
if (found && text[i + word_symbols]==' ')
{
word_in_text++;
}
}
printf("Founded %i %s as the word an is Not the some part of the other word\n", word_in_text,b);
if (word_in_text != 0)
{
char input_word[15];
printf("Enter the word which you want to insert for first entered word\nWarning:You have to enter the world which have %i symbols\n--->>", word_symbols);
cin >> input_word;
int in_word_symbols = 0;
for (int a = 0;a < 15;a++)
{
if (input_word[a])
{
in_word_symbols++;
}
else
{
break;
}
}
printf("The word which you have entered has %i symbols\n\n", in_word_symbols);
if (word_symbols == in_word_symbols)
{
for (int i = 0; i < 550 - word_symbols; i++)
{
int found = 1;
for (int j = 0; j < word_symbols; j++)
{
if (b[j] != text[i + j])
{
found = 0;
break;
}
}
if (found != 0 && text[i + word_symbols] == ' ')
{
for (int j = 0; j < in_word_symbols; j++)
{
text[i + j] = input_word[j];
}

}
}
printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text);
}
else if (in_word_symbols > word_symbols)
{
int text_char_index = 0;
step1:
for (int count = 0; count < 550 - word_symbols; count++)
{
text_char_index++;
int found = 1;
for (int j = 0; j < word_symbols; j++)
{

if (b[j] != text[count + j])
{
found = 0;
break;
}

if (found != 0 && text[count + word_symbols] == ' ')
{
count = text_char_index-1;
goto index_changing;
insert:
for (int c = 0; c < in_word_symbols; c++)
{
text[count + c] = input_word[c];
}
if (count > 500)
{
goto printing_result;
}
else
{
continue;
}
}
}
}
index_changing:

for (int l = 466; l > text_char_index + word_symbols; --l)
{
text[l] = text[l + 1];
}
goto insert;
printing_result:
printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text);
}
}
goto Start;

return 0;
}


If I enter the word was ( - it is word which i want to change in text) and than I enter the word detected (which has more symbols than first entered word - an it is the word which i want to insert in the text in return "was")


Console output is: There detected


And I can't use in this code method or something like that, I can only use if() and for loop.
Can anyone help me to understand how to do it?

Answer

First let's get the ground rules straight -- you are not allowed to use string functions/methods in this exercise, you have to write the code out explicity using primarily if statements and loops.

Rather than create a whole separate block of code to deal with the replacement word being larger than the original, just deal with it as part of the original replacement code -- as well as deal with the possibility that the replacement word is smaller. Both require shifting the original text one way or the other based on the difference between the original word and the replacement.

Here is a rework of your code that does the above and makes a number of style changes and bug fixes. It also makes it a pure 'C' program:

#include <stdio.h>
#include <stdbool.h>

int main()
{
    printf("This program can change word or character combinations in text.\n\n"); 

    char text[1024] =
        "There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\n"
        "Food was in plenty for them. They also wasted the bread, biscuits and fruits\n"
        "of the shop. The grocer got really worried. So, he thought I should buy a cat\n"
        "and let it stay at the grocery. Only then I can save my things. He bought a\n"
        "nice, big fat cat and let him stay there. The cat had a nice time hunting mice\n"
        "and killing them. The mice could not move freely now. They were afraid that\n";

    while (true) {
        printf("%s\n", text);

        int characters_in_text = 0;

        for (int f = 0; text[f] != '\0'; f++) // not allowed to use strlen()
        {
            characters_in_text++;
        }

        printf("Text has %i characters.\n\n", characters_in_text);

        char word[17];
        printf("Which word would you like to change in text above?\n");
        printf("(Maximum quantity of characters is %d)\n", (int) sizeof(word) - 2);
        printf("Type word or some character(s)\n");
        printf("--->> ");

        (void) fgets(word, sizeof(word), stdin);

        int a, word_characters = 0;

        for (a = 0; word[a] != '\n'; a++)
        {
            word_characters++;
        }

        word[a] = '\0';

        if (word_characters == 0)
        {
            break; // exit program
        }

        printf("Word which you have entered has %i characters.\n", word_characters);

        int words_in_text = 0;

        for (int i = 0; i < characters_in_text - word_characters; i++)
        {
            bool found = true;

            for (int j = 0; j < word_characters; j++)
            {
                if (word[j] != text[i + j])
                {
                    found = false;
                    break;
                }
            }

            if (found)
            {
                char next_letter = text[i + word_characters];

                if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0')
                {
                    words_in_text++;
                }
            }
        }

        printf("Found %i instance(s) of %s that are not part of another word.\n", words_in_text, word);

        char replacement_word[17];
        printf("Enter the word which you want to insert for first entered word.\n");
        printf("(Maximum quantity of characters is %d)\n", (int) sizeof(replacement_word) - 2);
        printf("Type word or some character(s)\n");
        printf("--->> ");

        (void) fgets(replacement_word, sizeof(replacement_word), stdin);

        int replacement_word_characters = 0;

        for (a = 0; replacement_word[a] != '\n'; a++)
        {
            replacement_word_characters++;
        }

        replacement_word[a] = '\0';

        printf("The word which you have entered has %i characters.\n\n", replacement_word_characters);

        int text_shift = replacement_word_characters - word_characters;

        for (int i = 0; i < characters_in_text - word_characters; i++)
        {
            bool found = true;

            for (int j = 0; j < word_characters; j++)
            {
                if (word[j] != text[i + j])
                {
                    found = false;
                    break;
                }
            }

            if (found) 
            {
                char next_letter = text[i + word_characters];

                if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0')
                {
                    if (text_shift > 0)
                    {
                        for (int k = characters_in_text; k > i + word_characters - 1; k--)
                        {
                            text[k + text_shift] = text[k];
                        }

                    }
                    else if (text_shift < 0)
                    {
                        for (int k = i + word_characters; k < characters_in_text + 1; k++)
                        {
                            text[k + text_shift] = text[k];
                        }

                    }

                    characters_in_text += text_shift;

                    for (int j = 0; j < replacement_word_characters; j++)
                    {
                        text[i + j] = replacement_word[j];
                    }
                }
            }
        }
    }

    return 0;
}

You can see how complicated this code gets without library functions -- testing for the possible punctuation that follows a word would be a lot easier with functions like ispunct() in ctype.h

You also computed a number of useful values in your original code but failed to use them appropriately later on in the code.

Yet to do: you've a lot of error checks to add to this code -- what if the replacement makes the text larger than the array that contains it? You check that a word isn't contained in another by testing the character after the word but fail to test the character before the work, e.g. 'other' vs. 'another'.