GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Dec-2008, 20:12
Allan Allan is offline
New Member
 
Join Date: Nov 2008
Posts: 14
Allan is on a distinguished road

Write a function name reverseCString with two formal input parameters


good morning guys,

we were tasked to write a function name reverseCString with two formal input parameters. Thes input parameters are pointers to char data. The function should not have any return value.

here is the function prototype :

void reverseCString( char *source, char *target );

The formal parmeter source is a pointer to the first character of the actual source C-String. The formal parameter target is a pointer to the first character of the target C-String. The charcters in the source C-String should be copied to the target C-String in reverse.

we are only allowed to use the iostream header file.

INPUT:
program should start by reading the number of input data. Let this number be N. The program then should read N C-Strings.

SAMPLE INPUT

4 heLLo THERE manny paqUIao

SAMPLE OUTPUT

heLLo : oLLeh
THERE : EREHT
manny : ynnam
paqUIao: oaIUqap

i have the code and it is working, i'm wondering if i follow the instruction carefully, and i want some comments to improve my code:

thank guys

here is the code

CPP / C++ / C Code:
#include <iostream>
using namespace std;

const int size = 1024;

void reverseCString( char *source, char *target )
{
   int sourceCount = 0; // this will count the number of character

   while( *source != NULL ) // statement will point the pointer source to 
   {                                // the last character of the c-string
      source++;
      sourceCount++;
   }

   while( sourceCount > 0 ) // loop until the all char been copied
   {
      source--; 
     *target = *source;  // copy the value of where ever the source pointer points to
      target++;  
      sourceCount--;
   }
}

int main()
{
	char data[ size ] = "";  
	char reverseData[ size ] = ""; // array of char wher the reversed copy wil be stored
 	int N = 0;

	cin >> N;

	for( int i = 0; i < N; i++ )
	{
	   cin >> data;
	   cout << data << " : ";
                reverseCString( data, reverseData );
	   cout << reverseData << endl;

	   for( int i = 0; i < size; i++ )
                   reverseData[ i ] = NULL;  // this will prepare all element for the next reading of C-String
	}
	return 0;
}

thanks guy,

your comments are highly appreciated
  #2  
Old 05-Dec-2008, 01:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Write a function name reverseCString with two formal input parameters


The program looks good. Possible changes:

The first while can be replaced with a single call to strlen(). All you're looking for is the length of the array.

The second while can be changed to a for which also allows you to get rid of one of your variables.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 05-Dec-2008, 06:13
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Write a function name reverseCString with two formal input parameters


Quote:
Originally Posted by WaltP
The first while can be replaced with a single call to strlen().

WaltP may need new reading glasses: we are only allowed to use the iostream header file.

Allan,

You don't need the length of the source string, rather, a pointer to it will work nicely:

CPP / C++ / C Code:
#include <iostream>

void reverseCString(const char* source, char* target)
{
    const char* p = source;

    while(*p) // find end of source
    {
        ++p;
    }
    --p; // move p back 1 from null terminator
    while(p >= source)
    {
        *target++ = *p--;
    }
    *target = '\0';
}

int main(int argc, char* argv[])
{
    char buf[1024] = {0};
    for(int i = 0; i < argc; i++)
    {
        std::cout << "original string: " << argv[i];
        reverseCString(argv[i], buf);
        std::cout << " reversed string: " << buf << std::endl;
    }
    return 0;
}

Output:

Code:
$ ./reverseCString hello world this is the original mexican bob original string: ./reverseCString reversed string: gnirtSCesrever/. original string: hello reversed string: olleh original string: world reversed string: dlrow original string: this reversed string: siht original string: is reversed string: si original string: the reversed string: eht original string: original reversed string: lanigiro original string: mexican reversed string: nacixem original string: bob reversed string: bob

Obviously, this code does not catch buffer overflow errors, which is "bad." However, the point of it is that you can use pointer arithmetic. It doesn't really save you any storage, but using a pointer instead of a "counter" indicates that you have an understanding of pointers, which may be helpful if scoring is involved.


El Mexicana Roberto Originales
  #4  
Old 05-Dec-2008, 20:39
Allan Allan is offline
New Member
 
Join Date: Nov 2008
Posts: 14
Allan is on a distinguished road

Re: Write a function name reverseCString with two formal input parameters


yeah that's right,

by the way thanks for your comments
i'l change my other statements to make it
simple,

about score, yeah it will make my score higher!!

thank you guys!!
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple questions for C++ project devster420 C++ Forum 1 20-Apr-2007 22:26
Message Class TransformedBG C++ Forum 5 29-Nov-2006 22:28
reverse a string using recursion varun51 C Programming Language 1 13-Oct-2006 19:31
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14
Reverse UTF encoded string maaz C++ Forum 2 13-Apr-2005 13:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:30.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.