Help!!!! String!!

This is a discussion on Help!!!! String!! within the C++ Programming forums, part of the General Programming Boards category; im beginer of the C+ in my text book it is the challenge program and it does not even have ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    5

    Help!!!! String!!

    im beginer of the C+ in my text book it is the challenge program

    and it does not even have answer....please help me out.. i tried to do like

    5 hours last night....no clue..

    Write a program that takes three strings (may be phrases): the text string, search string, and replacement string.The program outputs the modified text based on the entered strings -- by performing a call to the search_and_replace
    function above.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by icefire99 View Post
    5 hours last night....no clue..
    Post your latest code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    i guess you could use strstr or implement it some how ..
    here is an example to how to use strstr

    Code:
    #include <iostream>
    #include <string>
    
    
    int main ()
    {
      char str[] ="This is a simple string";
      char * pch;
      pch = strstr (str,"simplsimple");
      strncpy (pch,"sample",5);
      puts (str);
      return 0;
    }
    this code could give you sevral ideas such :
    1.
    watch how the source code of strstr works and rewrite in your own words.
    then just change what you need.
    2.
    copy paste what i did (and you will not learn a thing) but youll get your program.

    anyway ..
    try googling for strstr source code or read strinng.h
    i found few thing that could help :
    http://www.codeguru.com/Cpp/Cpp/stri...cle.php/c5641/
    http://www.tek-tips.com/viewthread.c...1173833&page=1

    what can i say i thound some thing nicer :
    http://lynx.isc.org/lynx2.8.5/lynx2-8-5/src/strstr.c
    and one for the last try using a better topic for example :
    how can i search a string inside a string
    Last edited by jabka; 04-22-2007 at 11:45 AM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string text, search, replacement, res;
    	getline(cin,text); getline(cin,search); getline(cin,replacement);
    	for (string::size_type pos = 0; pos < text.size(); pos++) {
    		if (text.find(search,pos) == pos) {
    			res += replacement;
    			pos += search.size()-1;
    		} else {
    			res += text[pos];
    		}
    	}
    	cout << res << "\n";
    	return 0;
    }
    I'm not sure if this is what you are looking for, but it may give you a better idea to how to approach the problem. What the code does is finds any matches from left to right, if it finds one - it replaces it and increments the counter to skip through the old text. Otherwise it just appends the current character. It will replace more than one, so if you had "hello world world" and your search is "world" and replacement is "hello" then you would get "hello hello hello". There are more efficient ways of doing this because you do some duplicate searches as well as unnecessary searches (i.e. searches whole string from current position every time). As an exercise you can modify it to use the substr() function and do a comparison to emulate the "find" which should make it faster.
    The cost of software maintenance increases with the square of the programmer's creativity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21