Help needed

This is a discussion on Help needed within the C++ Programming forums, part of the General Programming Boards category; I have written this code to swap the characters between the asterisks when a * is detected however it does ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    4

    Help needed

    I have written this code to swap the characters between the asterisks when a * is detected however it does not seem to work. Anyone? Take for example an input string "This is *swaped* here no change" will produce an output string " This is *depaws* here no change". Thanks.
    Code:
     
    #include <vcl.h>
    #include "MT262io.h"
    #pragma hdrstop
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    // Any declarations you require go here.
    AnsiString Sentence,Sentence1,Sentence2;
    int index,index1,Endindex;
    bool swap;
    
    // Your code goes here.
    index=1,index1=1,Endindex=1;
    swap=true;
    
    Sentence=ReadStringPr("Enter text to be tested.");
    
    while(index<=Length(Sentence))
    {
            if(swap==false)
            {
            Sentence1[index]=Sentence[index];
            index=index+1;
            }
            if(Sentence[index]=='*')
            {
            while(Sentence[index1]!='*')
            {
            index1=index1+1;
            }
            swap=true;
            }
            if(swap==true)
            {
            for(index=1; index<=10; index++)
            {
            Sentence[index]=Sentence2[Endindex];
            Endindex=Endindex-1;
            }
            swap=false;
            }
            }
            WriteString(Sentence1);
            WriteString(Sentence);
            Sentence.Insert(Sentence2,'*'+1);
      getchar();
      return 0;

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    For loop babe.

    Cycle through each character in an array, checking if that element == '*' .

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Try indenting the code better as well. Good indentation tells you a lot about how the code is behaving without having to do a lot of thinking about what every line is doing.

    Code:
            if(swap==false)
            {
            Sentence1[index]=Sentence[index];
            index=index+1;
            }
    // would be changed to
            if(swap==false)
            {
                    Sentence1[index]=Sentence[index];
                    index=index+1;
            }
    Also, start simple!.

    Your first attempt at this code should be something to identify the *'s
    Like
    Code:
    while(index<=Length(Sentence)) {
        if ( Sentence[index]=='*' ) {
        } else {
        }
    }
    The 'if' part would then toggle your 'swap' flag between true and false say.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Help Please
    By Moose112 in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2006, 06:16 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM

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