Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
I have reversed a character array. But I am getting some junk values displayed.
Sorry if this seems to be a dumb doubt..i am just starting c++...
PFB the code i have written to reverse a character array.

C++
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>

using namespace std;

int main()
{
   
    char a[10],b[10];
    int i,j,k,z;
    cout<<"enter a=";
    gets(a);
    cout<<"the array is-"<<a<<endl;
    i = strlen(a);
    k=i-1;
    for(j=0;j<i;j++)
    {

        b[j]=a[k];
        k--;
    }
    b[j]='/0';
    cout<<"reversed-"<<b<<endl
    return 0;
}


Output received is as follows:
the array is-manc
reversed-cnam0'?&9manc
As you could see the array is reversed as cnam was the reversed characters but it is followed by junk values.
What is the reason for these junk values? How to avoid them?

What I have tried:

I was able to display what i needed by simply using a for loop. But I wanted to know why the junk values are displayed and what is its significance.
Posted
Updated 10-Jun-17 11:57am
v2
Comments
PIEBALDconsult 10-Jun-17 12:06pm    
'/0' should be '\0'
[no name] 10-Jun-17 12:11pm    
5 even it does not Count
Afzaal Ahmad Zeeshan 10-Jun-17 13:12pm    
They do count... Virtually. :-)
Redmanc 10-Jun-17 12:17pm    
yep..that was the error..thank you!!

Two problems I see: both of which should prevent the code compiling.
1) null is backslash zero, not slash zero:
C++
b[j]='/0';
should be:
C++
b[j]='\0';

2) You are missing a semicolon on this line:
C++
cout<<"reversed-"<<b<<endl
Should be:
C++
cout<<"reversed-"<<b<<endl;

Since that code doesn't compile - or shouldn't in any sensible compiler - the results you get from running your code is irrelevant, as it isn't the code you are looking at!
Fix those two, and it should compile, and work correctly.
 
Share this answer
 
Comments
Redmanc 10-Jun-17 12:14pm    
Thanks a lot!!
OriginalGriff 10-Jun-17 12:18pm    
You're welcome!
There is a tool that allow you to see what your code is doing, its name is debugger.
It is also a great learning tool because it show you reality and you can see which expectation match reality.

With the debugger, you would have found yourself that
C++
b[j]='/0';

was not doing what you expected.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900