Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C++ Strings, instream, delete duplicates Posted by zergog on 19 Apr 2011 at 2:54 PM
Ok i need help i can't figure out how to instream into a string or the algorithm.




Write a C++ Program that uses:

Strings
Passing Strings to functions
Files Input and output.

Project Requirements:

Write a function called removeRepeats that, given a string, removes any repeated letters and returns a new string.
Write a main method that reads words (strings) from a file and writes the new strings (no repeated letters) to another file.
Several options are left for the programmer to determine:
Use of either c-string(char arrays) or the class strings .
Use of a container (array or vector) to store the strings. You can either store the strings in an array or vector - or just read and write without storage.
As you can see, many of the implementation details are left up to the programmer. At this point in the course you should be able to make design decisions based on good programming practices.



Sample input is a file that will be entered by a user and contains words. Note case should not matter but should be able to compare and delete. my example is

Google
Yahoo
Lie
Ggy

it should then out put like the following

Gogle
Yaho
Lie
Gy or gy
Report
Re: C++ Strings, instream, delete duplicates Posted by pseudocoder on 19 Apr 2011 at 8:46 PM
I won't really get into functions or files, but to remove the dups, you might try fiddling around with something like

#include <iostream>
#include <string>
#include <algorithm>

int main() {

   std::string strs[] = { "Google", "Yahoo", "Lie", "Ggy" };   
   
   for(int i=0; i < 4; ++i) {
      strs[i].erase(std::unique(strs[i].begin(), strs[i].end()), strs[i].end());
      std::cout << strs[i] << std::endl;
   }
   
   return 0;
}

Report
Re: C++ Strings, instream, delete duplicates Posted by zergog on 20 Apr 2011 at 5:53 AM
No your code is flawed the problem is to read in words from a file and then remove duplicate letters that are next to each out and then write that to another file
Report
Re: C++ Strings, instream, delete duplicates Posted by pseudocoder on 20 Apr 2011 at 1:11 PM
Flawed? It's not my assignment; bridge the gap between the snippit and what you need to do, which would seem to be handling files, functions, mixed case, and such. :)

If you're having trouble, post your attempt(s).



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.