Computer Science C++ – Write a function RevText() to read a text file “input.txt” and print only words (strings) starting with letter ‘i’ or ‘I’ in reverse order and rest of the words as it is.

This problem is related to Strings in Computer Science C++. Lets assume a text file named “input.txt” already exists. You can create a text file using any text editor – I use notepad++ for example and write the following text, for example:

India is my Country.

Expected output:

Your program should give the following output:

aidnI si my Country.

Note:

You can write any other piece of text or strings in a text file, make sure it has few words starting with letter “I” or “i”.

Program Code and Explanation

#include<fstream.h>
#include<string.h>
void RevText()
{
  char word[30],rword[30];
  ifstream ifile("input.txt");
  while(!ifile.eof())
  {
    ifile>>word;
    if(word[0]=='I' || word[0]=='i')
    {
      int i=0;
      for(int k=strlen(word)-1;k>=0;k--)
      {
        rword[i]=word[k];
        i++;
      }
      rword[i]=NULL;
      cout<<rword<<" ";
    }
    else
    {
      cout<<word<<" ";
    }
  }
}
void main()
{
  RevText();
}

Important key points – Handling string related problems

  • Include <fstream.h>, since we are performing file handling. (We are working with text files).
  • Include <string.h> since we are using strings handling function strlen() to calculate the length of the string.
  • We will be creating the function RevText() which does the entire job. In CBSE Computer Science exam you are usually asked to create a function.
  • Program assumes the file “input.txt” already exists with some text in it. Program does not performs any checks whether file exists or not. You can upgrade this program and accommodate that feature on your own.
  • Program reads the file word by word:
ifile>>word;
  • Next program checks if the first letter of the word i.e. word[0] is letter “I” (in upper case or lowercase form) or not.
if(word[0]=='I' || word[0]=='i')
  • If first letter of the word (word[0]) is “I” or ‘i‘ then program loops through the entire word and reverses the word and stores in another variable rword.
  • Observer, It stores NULL at the end of the reversed word (rword) and displays it.
  • Never forget to put NULL at the end of the string when you are constructing the string yourself as shown in the example.
if(word[0]=='I' || word[0]=='i') 
{ 
    int i=0; 
    for(int k=strlen(word)-1;k>=0;k--) 
    { 
        rword[i]=word[k]; 
        i++; 
    } 
    rword[i]=NULL; 
    cout<<rword<<" "; 
}
  • Otherwise displays the word itself (not the rword):
else 
{ 
    cout<<word<<" "; 
}
  • Function main() just calls the function RevText()

Conclusion

You can face similar string related problems in your board exams. Work more on looping constructs in order to be able to solve such string related problems.

 

Pawan Arora AdministratorKeymaster
Founder , Edukers
Teaching, Coding and Sharing is his passion. A true mentor and motivator. C/C++, Python, Java, Web Technologies (html5 / CSS/ Javascript/ JQuery,Bootstrap, nodeJS ,PHP etc.) and a WordPress enthusiast with more than two decades of experience.
follow me