c program for the reverse the digits

后端 未结 8 940
北恋
北恋 2021-01-07 11:27

I am looking for the C program for reverse the digits like below:

If i enter:

123456

Then the result would

8条回答
  •  星月不相逢
    2021-01-07 11:43

    Looks like a very old thread. But I referred the solutions here and had to cook up my own solution after testing the different programs available as solution on this website. I realized that treating the number as int will not yield necessary reversing of digit if the number ends with zero(s). So I decided to treat the number as a string and then reverse the digits. This way, I get exact reverse of the digit from a string standpoint and not a mathematical one that discards zeros at the beginning of a number.

    #include 
    #include 
    
    main()
    {
    
        char num[20];
        int x=0,slen;
    
        printf("Enter the number you want to reverse :");
        scanf("%s",num);
        slen=strlen(num);
    
    
        char *pointertoarray = &num[slen];
    
        for (x=slen;x>=0; x--){ printf("%c",*pointertoarray); pointertoarray--; }
        printf("\n");
    
    }
    

提交回复
热议问题