CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Reversing the digits

    //PROGRAM TO REVERSE THE 5 DIGIT NUMBER
    #include <stdio.h>
    void main()
    {

    int num, i = 0;

    char rev[5];

    printf("Enter any 5 digit number\n");

    scanf("%d", &num);
    i = 0;
    while(num)
    {


    rev[i] = num % 10;// IS THIS RIGHT WAY TO ASSIGN CHARACTER ARRAY?????

    i++;

    num = num/10;
    }

    printf("%s", rev);

    }

    What's wrong in the above program.

    WHILE DISPLAYING i am not getting correct output. whats wrong in rev[] array.

    PLEASE HELP!!!
    TANUSHREE-AGRAWAL...

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Reversing the digits

    Quote Originally Posted by Tanushreeagr View Post
    rev[i] = num % 10;// IS THIS RIGHT WAY TO ASSIGN CHARACTER ARRAY?????
    You're extracting the rightmost digit from the integer held in num. It will be an integer between 0 and 9. What you want to do now is to convert it to a char between '0' and '9' before assigning it to the array. Since those characters lie next to each other in the ASCII encoding this will work (although it isn't that elegant)

    rev[i] = '0' + num % 10;

    http://www.asciitable.com/

    The ASCII char '0' is represented by 48 which is added to the digit between 0 and 9 turning it into an ASCII char between '0' and '9'.
    Last edited by nuzzle; March 9th, 2013 at 02:41 AM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing the digits

    If you just want to reverse an integer, see this thread

    http://forums.codeguru.com/showthrea...ighlight=emirp

    Code:
    //Reverse an integer
    int ReverseInteger(int no)
    {
    int rnum = 0;
    
    	while (no) {
    		rnum = rnum * 10 + (no % 10);
    		no /= 10;
    	}
    
    	return rnum;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing the digits

    PS. When you post code, please use CODE tags for readability. Go Advanced, select code then click '#'
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Reversing the digits

    Quote Originally Posted by 2kaud View Post
    If you just want to reverse an integer, see this thread

    http://forums.codeguru.com/showthrea...ighlight=emirp

    Code:
    //Reverse an integer
    int ReverseInteger(int no)
    {
    int rnum = 0;
    
    	while (no) {
    		rnum = rnum * 10 + (no % 10);
    		no /= 10;
    	}
    
    	return rnum;
    }
    I considered that approach as well, but whether it actually is valid depends on the exact requirements set up by the assignment: It behaves different from the char[]-based approach when the subject number ends with one or more zeroes. These would become insignificant in processing and thus omitted in the output. That was irrelevant in the emirp context since there are no primes ending with 0 anyway.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: Reversing the digits

    Here are some fixes to your code. Study it and my comments.

    Quote Originally Posted by Tanushreeagr View Post
    Code:
    //PROGRAM TO REVERSE THE 5 DIGIT NUMBER
    #include <stdio.h>
    void main()
    {
    	
    	int num,  i;  // Initializing i here is not needed since it is done later.
    
    	char rev[6]; // You need to allocate space for the null character to terminate the string.
    	             // This is required by the printf() function.
    
    	printf("Enter any 5 digit number\n");
    
    	scanf("%d", &num);
    	i  =  0;
    	while(num & (i < 5)) // It is very important that you check that the number is not too long.
    	                     // Without this check data could be written outside of the rev array causing memory corruption.
    	{
    
    		rev[i]  =  '0' + (num % 10);
    
    		i++;
    
    		num  =  num/10;
    	}
    	rev[i] = '\0';  // Make sure you terminate the string with a null character.
    
    	printf("%s\n", rev);   // This prints a new line after the result.
    
    }

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reversing the digits

    ..and some further comments.

    To make the revised program work, the while condition test should be the logical and not the bitwise and.

    Code:
    while(num && (i < 5))
    Also it would be good practice to declare and initialise i as close as possible to where it is first used (or at least initialise it where it is defined).

    main() should return an integer by the ANSI standard.

    Code:
    //PROGRAM TO REVERSE THE 5 DIGIT NUMBER
    #include <stdio.h>
    
    int main()
    {
    char	rev[6];		// You should allocate space for a null character to terminate the string.
    
    int	num;
    
    	printf("Enter any 5 digit number: ");
    	scanf("%d", &num);
    
    int	i = 0;
    
    	while (num && (i < 5))	// It is very important that you check that the number is not too long.
    				// Without this check data could be written outside of the rev array causing memory corruption.
    	{
    		rev[i++]  =  '0' + (num % 10);
    		num  /=  10;
    	}
    	rev[i] = '\0';		// Make sure you terminate the string with a null character.
    
    	printf("%s\n", rev);	// This prints a new line after the result.
    	return (0);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Feb 2013
    Location
    United States
    Posts
    56

    Re: Reversing the digits

    Quote Originally Posted by 2kaud View Post
    To make the revised program work, the while condition test should be the logical and not the bitwise and.
    Doh!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured