Count number of occurrences of a character and search it in a file..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isabelle
    New Member
    • Dec 2006
    • 14

    Count number of occurrences of a character and search it in a file..

    hi, every body..

    I have two program I couldn’t solve them
    So, can any body help me. please!!


    1-Write a program that accepts a character and count number of occurrences in a file.
    The file should have some text, and the program count how many times the inputted character repeated in the file and prints the result on the screen.
    (Hint: you have to use continue statement in your solution )

    Sample input:
    Input.txt
    I am a student in the university

    Sample output:
    Enter the character :a
    The letter a repeated 2 times(s)


    2-Write a program that accepts a character and search it in a file.
    The file should have some text, and the program search for the first occurrence of the character and print it’s location on the screen…
    (Hint: use break statement to stop the loop)

    Sample input:
    Input.txt
    I am a student in the university

    Sample output:
    Enter the character to search :a
    the first occurrence of a character is at location 3

    I am waiting the replies ..

    { thanks all}
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    And your question is....

    Comment

    • isabelle
      New Member
      • Dec 2006
      • 14

      #3
      Originally posted by weaknessforcats
      And your question is....
      The code for first program AND the other program like the first one BUT the algorithm(inden tation )is different and we must use the break statement :

      Code:
      #include<iostream>
      #include<fstream>
      using namespace std;
      int main( )
      {
      int count=0;
      char letter;
      
      ifstream infile;
      infile .open("Input.txt");
      if(! infile)
      {
      cout<<"cannot open the input file."<<endl;
      return 1;
      }
      infile>>letter;
      cout<<"Enter the character:";
      while(! infile.eof ( ))
      {
      ............................... 
      ...............................
      .............................
      continue;
      count++;
      }
      
      infile.close( );
      return 0;
      
      }
      My question is what should be the algorithm(inden tation ) in the blank space in two programs????

      {thanks all}

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I'm not sure what line 16 is for.

        However, you should:
        1) ask for a character from the user
        2) enter your loop and read a char from the file.
        3) compare the char to the one the user entered.
        4) if equal, increment the occurrence count
        5) go back to 2.

        I'm not supposed to give you the exact code.

        Hint: you might want to use cin.get() rather than cin>>.

        Comment

        • isabelle
          New Member
          • Dec 2006
          • 14

          #5
          hi...

          where your replies??

          I am really need help!!

          please, help me??

          thanks all

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by isabelle
            hi...

            where your replies??

            I am really need help!!

            please, help me??

            thanks all
            Post #4 in this thread

            Comment

            Working...