working with textfiles in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajenia
    New Member
    • Oct 2006
    • 8

    working with textfiles in c++

    a text file contains a number of English sentences and each line in the file holds just one sentence. consider a class which will perform the following:
    1 reads these sentences and stores them in an array
    2 displays the sentences from the array created in 1
    3 sorts the sentences into alphabetical order
    4 given a word, determines how many times the given word occurs in all the sentences.

    please, any one out there with an idea!
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by sajenia
    a text file contains a number of English sentences and each line in the file holds just one sentence. consider a class which will perform the following:
    1 reads these sentences and stores them in an array
    2 displays the sentences from the array created in 1
    3 sorts the sentences into alphabetical order
    4 given a word, determines how many times the given word occurs in all the sentences.

    please, any one out there with an idea!
    Do you have written any code to start with? I don't think there will be too many people solving your assignments for you ... but there will be people willing to help if you come up with concrete problems or questions, I guess :-)

    Comment

    • D_C
      Contributor
      • Jun 2006
      • 293

      #3
      Pseudocode for #1
      Code:
      open text file
      // it would be nice if it told you how many lines to make it
      while(not end of file)
      {
        str = get next line
        if(array full)
          make a bigger array
        insert str into next open spot in array
        increment index for next open
      }
      close the text file
      Pseudocode for #2
      Code:
      for(int i = 0; i < NEXT_OPEN_ENTRY; i++)
      {
        output each sentence
      }
      Pseudocode for #3
      Code:
      for(each array entry)
      {
        select a default "minimum" string.
        for(each unsorted array entry)
        {
          compare the minimum string to each entry
          if each entry is alphabetically less than the current minimum
            then it's your new minimum
        }
        swap the minimum string with the default
      }
      You still have to figure out how to compare two strings, which isn't too bad. There should be a method of some sort to do it for you.

      Pseudocode for #4
      Code:
      read in the word to search for
      initialize a counter to zero
      for(each string)
      {
        while(not end of string)
        {
          while(not the end of the string) 
          {
            compare it with pattern
            if there isn't a match
             then fast forward to non-alphabetic characters
          }
          if a complete match
            increment the counter
          while(non-alphabetic characters)
            fast forward to alphabetic characters
      }

      Comment

      Working...