here's a function I tried to write that reverses the order of a c-string

void reverse(char *wordPtr)
{
	char revWord[SIZE]; 
	int count = 0,			// index counter 
		newSize = 0;		// to count word size

	while (wordPtr[count] != '\0')
	{
		newSize += 1; 
		count++;
	}

	cout << "The size of the string you entered is " << newSize << endl; 

	for (count = 0; count < newSize; count++)
	{
		revWord[newSize - (count + 1)] = wordPtr[count];
	}

	cout << "the word backwords is " << revWord 
                << endl; 
}

wordPtr points to a character array in the main function

if I input "Hello" or anything else my output always has extra characters on the end. How do I get rid of those?

Recommended Answers

All 13 Replies

Use code tags!
When you see extra characters after what it is suppost to print out, it's probably becouse you forgot to put the null terminator after your string. Also, where is the variable SIZE defined?

SIZE is a global constant

How about reversing in place

void reverse(char *wordPtr)
{
  char c, *dstPtr;

 dstPtr = wordPtr + strlen( wordPtr ) - 1;
  while (wordPtr < dstPtr)
  {
     c = *wordPtr;
     *wordPtr++ = *dstPtr;
     *dstPtr-- = c;
  }

How about reversing in place..............

Yes, but you'll need a closing curly brace (on the last line) in order to make the code compile :P

Add a null character at the end of the reversed string :

for (count = 0; count < newSize; count++)
	{
		revWord[newSize - (count + 1)] = wordPtr[count];
	}

	revWord[newSize] = 0;

}

Also, instead of

while (wordPtr[count] != '\0')
	{
		newSize += 1; 
		count++;
	}
}

you could use the strlen() function

newSize = strlen(wordPtr);
}

An addition for the OP:
Also ensure that the variable newSize is of type size_t.
Strings don't have negative lengths, so using int isn't the best option here (because it's signed, this means that it can hold negative values).

Okay so I missed the closing brace since it was late when I posted it.


{I'm not sure if the other postings were meant for me, but just in-case!}

As to the NULL terminator, my function didn't need one as it already existed. I was merely swapping characters.

In my case it didn't matter about size as no matter what the char type was, 8-bit, 16-bit {Single-Byte, Multi-Byte, Wide Char} the entire character was being swapped!

As to the unsigned issue. I ALWAYS in my code use unsigned int (uint) as all integers should be considered unsigned unless the special case comes up that they are signed. But apparently professors don't teach that and I've found from postings that it confuses the students as "My Professors says not to use that!" or "My Professor hasn't taught us that yet, how do I do it without that part!"

I've found newly graduated students have this same problem as to signed-unsigned, and are also oblivious as to why they need to check a negative value "As it never occured so why check for it!"

Now try a recursion functions for this job.

1) Find a base case.
2) Find a pattern.
3) Recurse pattern until base case is reached.

Also no need to worry about the little details right now. Just keep it in mind.

To Wildgoose:

doesn't strlen return an int value.
if so how come you can add it to a hexadecimal data type?

strlen returns a unsigned int. And you can add a decimal to hex.

for example :

int a = 0xe; //14
     cout<<a<<endl; //display 14
    a = a + 1;  //now a = 15 or 0xf
   cout<<a<<endl; //display 15

The computer merely sees a sequence of ones and zeros of a particlar sized grouping of bits.

For example 8-bit

10100101 Binary This is considered base 2
But that number can be represented by other number base systems. In grade school you were taught base 10.
As an unsigned 8-bit value this is seen as 165 decimal. As signed (-90).
In Hexadecimal, Hex for short A5 written as 0xA5 or 0A5h.
In Octal (245).

Adding one is no problem. A5 becomes A6. 165 becomes 166, etc.

In a higher language such as C where data has a type such as int versus unsigned int, the compiler will repute unlike data types being summed, but once they are cast to the same type, its simple math.

.

thanks everyone. I ended up using the NULL terminator. I still don't really understand wildgoose's post. I'll come back in a couple weeks when I've learned more and look at it again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.