Write a short program thats reads a file called text.txt that replaces all words that begins with the letter f and is replaced with the word frog. Now i have a jeist of how to replace words with another word however i am unsure how to find words only starting with f and replacing the string.

This is what ive gotten so far which isnt correct:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;
void get_input_from_user(string tag,string &input);
void replace_string(string in_text, string find_word, string replace_word,string &out_text);

int main()
{
   ifstream inFile;
   string infilename = “source.txt”;
   string line = “”;
   string input = “”;
   string in_text = “”;
   string find_word =“”;
   string replace_word = “”;
   string out_text = “”;


   inFile.open(infilename.c_str());
   if (inFile.fail())
     {
      cout << “The file “ << infilename;
      cout << ” cannot be opened for reading.”<<endl;
      cout << “Exiting.”<<endl;
      exit(1);
     }
   while(getline(inFile,line))
     in_text = in_text + line;

   inFile.close();
   cout << “Give me a string to find: “;
   getline(cin,find_word);
   cout << “Give me a replacement string: “;
   getline(cin,replace_word);
   replace_string(in_text, find_word,replace_word,out_text);
   cout << out_text;


}

void replace_string(string in_text, string find_word, string replace_word,string &out_text)
{
   int start_loc = 0;
   int char_num = 0;
   ostringstream outString;
   while(char_num < in_text.length())
        {
           start_loc = in_text.find(find_word,char_num);
           if (start_loc != string::npos )
             {
                outString << in_text.substr(char_num,start_loc);
                outString << replace_word;
                char_num = start_loc+find_word.length();
             }
           else
             {
                outString << in_text.substr(char_num,in_text.length()-char_num);
                char_num = in_text.length();
             }
        }
   out_text = outString.str();
   return;
}

You can check the 1st letter of a string like an array, that is

string test = "family";

so test[0] would be 'f', test[1] would be a, & so on, so generally just test the 1st letter of a string that way for if it is an f or F.

>i am unsure how to find words only starting with f and replacing the string.
You might find it easier to work through the source string character by character. This is really the realm of regular expressions, but an algorithm for matching the first character of each word isn't difficult:

for i = 0 to text.length
  # Find the next word
  while i < text.length and text[i] = whitespace do
    result = result + text[i]
    i = i + 1
  loop

  # Replace the word if necessary
  if text[i] = find[0] then
    result = result + find

    # Skip the source word
    while i < text.length and text[i] <> whitespace do
      i = i + 1
    loop
  else
    # Copy a non-match verbatim
    while i < text.length && text[i] <> whitespace do
      result = result + text[i]
      i = i + 1
    loop
  endif
loop

im so confused does this coincide with the 2nd post with string test etc?

I am unsure how to write the coding or start =/

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	ifstream ifile;

	ifile.open("text.txt");

	string words;

	while(ifile >> words)
	{
		if(words[0]== 'f')
		{
			// if the first letter is if 
			// here you can do the replacing 
		}
	}
	
	return 0;
}

give it a try and see!

Look at the sentence: Well,find me. Are you sure that whitespaces only are valid word separators?

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.