Member Avatar for Zaina jee

I have to write a program that will input a text paragraph from the user. The maximum size of the string will be 500.I have to write a program in which if user select 'R' or 'r' then user is asked to enter a word he wants to replace and another word that he wants the original word to be replaced with. If the original word exists, it will be replaced with the other word, otherwise word not found message will be displayed.

If user select 'I' or 'i' then the user will be asked to enter an index of the text string at which he/she wants to insert a word. Then he/she is asked to enter the word that he/she wants to insert. The text at the given index is shifted right and the word is then inserted at the given Index. Make sure that the text string has enough space to accommodate the new word. If there is not enough space then Not enough space message should be displayed
When ‘S’ or ‘s’ is entered, All special characters other than ‘.’ and spaces, and all digits will be deleted from the string. Moreover if multiple consecutive spaces exist between words then all those extra spaces will also be removed from the text.

Okay. So what do you have so far?

Start by writing a function that will replace words in a string and use a small program to test it in the absence of user input. Your program can be broken down into smaller subproblems that are easier to solve and less overwhelming.

Member Avatar for Zaina jee

Actually I din't understand the logic that how to write code for it. I only understand that for deleting special characters I have to check ASCII code for alphabets but how can I remove more than one space. I didn't understand it. I merely want to know logic that how could it be implemented.

Take at look at this and see how it can help you out.

Member Avatar for Zaina jee

I have write a function to find word entered by user so that I can replace it. But I didn' t understand that how can I get the starting index of that word?

int Findstring
(char para[])
    char fstr[20]="";
    int flen,index=0;
    char *token="";
    cout<<"Enter a string to find in paragraph: ";
        token =strtok(para," ");
   return index;

I wouldn't recommend using strtok() in this case because it modifies the string by way of inserting null characters at the end of each token. This effectively destroys the string for your purposes, so unless you're working with a copy, it's best to stick with a little bit more manual method:

#include <cstring>
#include <iostream>
#include <string>

size_t find_word(const char *s, const char *word)
    size_t len = strlen(word);

    for (const char *p = s; *p; ++p) {
        if (strnicmp(p, word, len) == 0)
            return (size_t)(p - s);

    return (size_t)-1;

int main()
    std::string line, word;

    std::cout<<"Enter a line of text: ";
    getline(std::cin, line);

    std::cout<<"Enter a word to find: ";
    getline(std::cin, word);

    size_t index = find_word(line.c_str(), word.c_str());

    if (index == (size_t)-1)
        std::cout<<"'"<< word <<"' was not found\n";
        std::cout<<"'"<< word <<"' is at index "<< index << '\n';

Note that I didn't do anything to replace since you only asked about finding the index of the word in question. I assume you already have ideas for doing the replacement once you have that index?

Also note that stricmp() and strnicmp() are not standard functions, so a compiler isn't required to implement them. However, since you used stricmp() in your code, I'm assuming your compiler does implement them.

Member Avatar for Zaina jee

Thanks Deceptikon. I have made program and your code really help me a lot.

Member Avatar for Zaina jee

Thanks Nathan Oliver and Deceptikon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.