:) Help.. Please... I need some hints

This is a discussion on :) Help.. Please... I need some hints within the C++ Programming forums, part of the General Programming Boards category; This is the Last two questions in my Homework, Please give em some hints... 1. Write afunction named replace that ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Question :) Help.. Please... I need some hints

    This is the Last two questions in my Homework,
    Please give em some hints...

    1. Write afunction named replace that takes three strings arguments:
    void replace ( string bas, string pattern, string replacement);

    this function should replace all instances of the pattern in the base string. Note that the replacement string may not be the same lenghth as the pattern should not be replaced. For example, if the base string is " all in good time," the pattern " good," and the replacement " very good," your procedure should avoid going into an infinite loop..

    That was the second from the last ... I gave up thinking about this one....

    I will give it to you... while I will be thinking about the last one...

    Please help me.... I will be helping some guys in the future...

    Thank you.... ALL
    C++
    The best

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    what you should do is

    search the instance "good" in the 'base' string by reading 4 chars at a time from 'base' and incrementing the counter [starting from 0th element] by 1 till end.. like first compare
    "all " with good then
    "ll i", then
    "l in", then
    " in ", and so on till "time". [dont forget the spaces]

    when you reach "good" and insert the word "very "[including spaces]. remember the counter position where the match took place and then move that counter by 9 ['very good' is 9 char long] otherwise you'll be comparing it again and be stuck there.

    use strlen to check the length etc,

    PS. sorry if this looks untidy, complicated etc but thats how i'll go about doing it.


  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Thanks.. I will try it

    I will try it right now

    Thanks anyway...
    C++
    The best

  4. #4
    Unregistered
    Guest
    the insert and replace functions in string class of STL are probably not what your instructor had in mind.

    Are you using an array of char or a list of char to represent the string? The advantage of the list is easier insertion (no shifting of char needed). The advantage of the array is easier element by element access. I suspect the array option is the one most people will use. If so, be sure the string you are removing from has enough room to hold the desired insertion(s).

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    You could also use the isspace() function in ctype.h to determine where a word ends..

    Code:
    char tempString = new char[strlen(string)];
    for (int i = 0; i < strlen(string); i++)
    {
    	if (isspace(string[i])
    	{
    		cout << tempString;
    
    		for (i = 0; i < strlen(tempString); i++)
    		{
    			tempString[i] = ' ';
    		}
    
           	} else {
                 		tempString = strcat(tempString, string[i]);
    
    
    	}
    }
    I haven't checked this code.. but you can probably see the logic behind it. This *should* print out each word in a string

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    That really should do it...

    because every string is an array of char... WE can do that....

    But you know what ... I was asking if there is any OOP way to do it without going pack to the c programming to do it in the old 60 fation way.....

    But at least we got one way to go..
    thaks Dual andeverybody ....
    C++
    The best

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    22

    *sigh*

    wish my homework was directly related to programming

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    it Did work beleive me .,,,,

    Thanks all for your help ...
    it did work ....!!!!

    That was great....
    thanks for everything guys...
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hints needed to start developing for WinCE
    By BrownB in forum Tech Board
    Replies: 0
    Last Post: 05-17-2006, 09:14 AM
  2. Hints to write a program
    By Bnchs400 in forum C++ Programming
    Replies: 28
    Last Post: 04-05-2006, 05:35 AM
  3. Can you give me some hints?
    By mag_chan in forum C Programming
    Replies: 6
    Last Post: 12-05-2005, 03:04 PM
  4. Can you give me some hints?
    By mag_chan in forum C++ Programming
    Replies: 1
    Last Post: 12-04-2005, 01:12 AM
  5. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 05:27 PM

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