Hellp My Code!!!! Doesnt Work!!

This is a discussion on Hellp My Code!!!! Doesnt Work!! within the C++ Programming forums, part of the General Programming Boards category; Code: #include "iostream" #include "string" using namespace std; void search_and_replace(char *text, char *search, char *replace); int main() { char text[256] ...

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

    Hellp My Code!!!! Doesnt Work!!

    Code:
    #include "iostream"
    #include "string"
    using namespace std;
    
    void search_and_replace(char *text, char *search, char *replace);
    
    int main()
    {
           char text[256] = "You can find and replace any part of this line using this program.";
           char originalText[256];
    
           cout << "Your text is " << endl;
    
           cout << text << endl << endl;
    
           strcpy(originalText, text);
    
           cout << "search_and_replace(text, \"find\", \"search\")" << endl;
    
           search_and_replace(text, "find", "search");
    
           cout << text << endl << endl;
         
           strcpy(text, originalText);
    
           cout << "search_and_replace(text, \"and\", \"&\")" << endl;
    
           search_and_replace(text, "and", "&");
    
           cout << text << endl << endl;
    
           // ...
    
           strcpy(text, originalText);
    
           cout << "search_and_replace(text, \"this line\", \"a sentence\")" << endl;
    
           search_and_replace(text, "this line", "a sentence");
    
           cout << text << endl << endl;
    
           strcpy(text, originalText);
    
           cout << "search_and_replace(text, \"this\", \"the\")" << endl;
    
           search_and_replace(text, "this", "the");
    
           cout << text << endl << endl;
    
           return 0;
    }
    
    void search_and_replace(char *text, char *search, char *replace)
    {
           char *pStart;
           char *pTemp;
           char *pOriginal;
    
           pOriginal = text;
    
           if ( (pStart = strstr(text, search)) == NULL )
           {
                 cout << "No search string found." << endl;
                 return;
           }
    
           pTemp = new char[(pStart-text)+strlen(replace)+(strlen(text)-strlen(search))+1];
           strncpy(pTemp, text, (pStart-text));
    
           pTemp[pStart-text] = '\0';
    
           strcat(pTemp, replace);
    
           text += (pStart-text)+strlen(search);
    
           strcat(pTemp, text);
           text = pOriginal;
    
           strcpy(text, pTemp);
    
           delete pTemp;
    
    }
    Last edited by Salem; 04-24-2007 at 05:02 AM. Reason: Fix broken use of code tags, remove pointless blank lines

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You might want to use code tags and format your code correctly, with indentations, then we might even consider reading it. And "my code doesn't work" isn't really a good description of the problem, that's like saying "my car isn't working", there could be tons of different problems.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Your topic names to date:

    • Help!!!! String!!
    • Hellp My Code!!!! Doesnt Work!!


    In this topic you just dumped your code without formatting it properly (ie. using code tags) or even telling us exactly what isn't working.

    Read my signature, and then read this:

    http://www.catb.org/~esr/faqs/smart-questions.html

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    It's not enough to simply scatter [code][/code] tags liberally through the post just to get the post checker to STFU, you have to understand what it's trying to tell you.

    So rather than getting an answer, there are now 3 replies discussing the meta issues of how to make a good post in the first place, rather than what you want to know.

    So spend more time thinking about the presentation of your posts in future - meaningful titles, an actual question, and nicely formatted code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    You should also try to comment your code a bit?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. this code COMPILES, but DOESNT WORK
    By Leeman_s in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2002, 05:54 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM

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