Logic to reverse string in c programming

Thread Starter

Parth786

Joined Jun 19, 2017
642
I am able to get reverse number of original number. I can write program for reverse number
C:
#include<stdio.h>

int main (void)
{
    int original_number;
    int reverse_number = 0;
  
    printf("Promote user to enter  number \n");
  
    scanf("%d",&original_number);
  
    while(original_number != 0)
    {
        reverse_number = reverse_number *10;
      
        reverse_number = reverse_number + original_number % 10;
      
        original_number = original_number /10;
    }
  
    printf("Reverse number is = %d \n", reverse_number);
  
    return 0;  
}
Promote user to enter number
786
Reverse number is = 687

I want to write program for reverse string without string.h header file. I have seen this link https://www.programmingsimplified.com/c-program-reverse-string
https://www.programmingsimplified.com/c-program-reverse-string
I don't understand logic of reverse string. What is logic for reverse string in c programming ?
 

WBahn

Joined Mar 31, 2012
29,843
Do you understand the logic of your reverse number code?

If someone were to look at your code and say that they didn't understand it and asked you to explain how it works to them, what would you tell them?
 

spinnaker

Joined Oct 29, 2009
7,830
I find, the best way to understand the logic of some code not familiar to you is to use the debugger and step through the code. There are even online compilers / debuggers for C that you can use for samll programs.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Do you understand the logic of your reverse number code?

If someone were to look at your code and say that they didn't understand it and asked you to explain how it works to them, what would you tell them?
reverse program
C:
#include<stdio.h>

int main (void)
{
    int original_number;

    int reverse_number = 0;

    printf("Promote user to enter  number \n");

    scanf("%d",&original_number);

    while(original_number != 0)
    {

        reverse_number = reverse_number *10+ original_number % 10;
     
        original_number = original_number /10;
 
    }
       printf("Reverse number is = %d \n", reverse_number);

    return 0;
}
Promote user to enter number
786
Reverse number is = 687

Explanations :
Original number = 786 , reverse number = 0

First pass
reverse number = reverse_number *10+ original_number % 10 = 0 * 10 +786 %10 = 0 + 6 = 6
original number = original_number /10 = 786 / 10 = 78

Second pass
reverse number = reverse_number *10+ original_number % 10 = 6 * 10 +78 %10 = 60 + 8 = 68
original number = original_number /10 = 78 / 10 = 7

Third pass
reverse number = reverse_number *10+ original_number % 10 = 68 * 10 +7 %10 = 680 + 7 = 687
original number = original_number /10 = 7 / 10 = 0

Reverse number is = 687

if you see above procedure correct then tell me logic to reverse string "parth" = "htrap"
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
reverse program
C:
#include<stdio.h>

int main (void)
{
    int original_number;

    int reverse_number = 0;

    printf("Promote user to enter  number \n");

    scanf("%d",&original_number);

    while(original_number != 0)
    {

        reverse_number = reverse_number *10+ original_number % 10;
    
        original_number = original_number /10;

    }
       printf("Reverse number is = %d \n", reverse_number);

    return 0;
}
Promote user to enter number
786
Reverse number is = 687

Explanations :
Original number = 786 , reverse number = 0

First pass
reverse number = reverse_number *10+ original_number % 10 = 0 * 10 +786 %10 = 0 + 6 = 6
original number = original_number /10 = 786 / 10 = 78

Second pass
reverse number = reverse_number *10+ original_number % 10 = 6 * 10 +78 %10 = 60 + 8 = 68
original number = original_number /10 = 78 / 10 = 7

Third pass
reverse number = reverse_number *10+ original_number % 10 = 68 * 10 +7 %10 = 680 + 7 = 687
original number = original_number /10 = 7 / 10 = 0

Reverse number is = 687

if you see above procedure correct then tell me logic to reverse string "parth" = "htrap"

You have translated what the program does but you did not show why it works. What does the % operator do? Do you know?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You have translated what the program does but you did not show why it works. What does the % operator do? Do you know?
module operator give reminder value
C:
#include<stdio.h>
int main (void)
{
    int a =786;
    int b = 10;
    int c = a% b;
  
    printf(" reminder is  =  %d  \n", c);
  
    return 0;
}
reminder is = 6

Hope we will come back on original question
 
Last edited:

WBahn

Joined Mar 31, 2012
29,843
reverse program
C:
#include<stdio.h>

int main (void)
{
    int original_number;

    int reverse_number = 0;

    printf("Promote user to enter  number \n");

    scanf("%d",&original_number);

    while(original_number != 0)
    {

        reverse_number = reverse_number *10+ original_number % 10;
    
        original_number = original_number /10;

    }
       printf("Reverse number is = %d \n", reverse_number);

    return 0;
}
Promote user to enter number
786
Reverse number is = 687

Explanations :
Original number = 786 , reverse number = 0

First pass
reverse number = reverse_number *10+ original_number % 10 = 0 * 10 +786 %10 = 0 + 6 = 6
original number = original_number /10 = 786 / 10 = 78

Second pass
reverse number = reverse_number *10+ original_number % 10 = 6 * 10 +78 %10 = 60 + 8 = 68
original number = original_number /10 = 78 / 10 = 7

Third pass
reverse number = reverse_number *10+ original_number % 10 = 68 * 10 +7 %10 = 680 + 7 = 687
original number = original_number /10 = 7 / 10 = 0

Reverse number is = 687

if you see above procedure correct then tell me logic to reverse string "parth" = "htrap"
Okay. If that's the level of explanation that you feel should be adequate for someone who doesn't understand how your program works, then that should be the level of explanation that you need to understand someone else's.

C:
#include <stdio.h>

int main()
{
   char s[1000], r[1000];
   int begin, end, count = 0;

   printf("Input a string\n");
   gets(s);

   // Calculating string length

   while (s[count] != '\0')
      count++;

   end = count - 1;

   for (begin = 0; begin < count; begin++) {
      r[begin] = s[end];
      end--;
   }

   r[begin] = '\0';

   printf("%s\n", r);

   return 0;
}
begin = 0; end = 0; count = 0;
Input a string: s[] = "parth"

Calculate length:
Pass #1: s[0] is 'p' -> count = 1
Pass #2: s[1] is 'a' -> count = 2
Pass #3: s[2] is 'r' -> count = 3
Pass #4: s[3] is 't' -> count = 4
Pass #5: s[4] is 'h' -> count = 5
Pass #6: s[5] is '\o' -> DONE

end = count - 1 = 4

Pass #1: begin = 0; 0 < 5: r[0] = s[4] = 'h'; end = 3; begin = 1
Pass #2: 1 < 5: r[1] = s[3] = 't'; end = 3; begin = 2
Pass #3: 2 < 5: r[2] = s[2] = 'r'; end = 2; begin = 3
Pass #4: 3 < 5: r[3] = s[1] = 'a'; end = 0; begin = 4
Pass #5: 4 < 5: r[4] = s[0] = 'p'; end = -1; begin = 5
Pass #6: 5 < 5: DONE

r[5] = '\0'

printf("%s\n", r); => htrap

Now, if that's sufficient for you to understand what it going on (and it should be, since that is the level of explanation you think is sufficient for others), then the next question you need to consider is what is preventing you from walking through the code and doing the same thing I just did?
 

WBahn

Joined Mar 31, 2012
29,843
module operator give reminder value
C:
#include<stdio.h>
int main (void)
{
    int a =786;
    int b = 10;
    int c = a% b;
 
    printf(" reminder is  =  %d  \n", c);
 
    return 0;
}
reminder is = 6

Hope we will come back on original question
The point is that to understand how an algorithm does what it does, it is necessary not only to know, for instance, that the remainder operator gives the remainder, but much more importantly, you need to know WHY that is of any value to the algorithm. The remainder operator is used to accomplish an objective that is useful for achieving the overall goal. How that objective is accomplished is really beside the point. There are other ways of accomplishing the same objective that do not use the remainder operator. So the real question is do you understand -- can you articulate -- what the objective is that the use of the remainder operator accomplished?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
There are other ways of accomplishing the same objective that do not use the remainder operator. So the real question is do you understand -- can you articulate -- what the objective is that the use of the remainder operator accomplished?
What you said is absolutely right.

I tried to find new way. I was thinking about from last 20 minutes But I could not find any other way.

One way I think like this
C:
#include <stdio.h>

int main(void)
{

  /* code */

   return 0;
}

int reverse (int number)

{
     /* code */

  return reverse number;
}
Function with return value and passing number. function will return reverse number

What could be the possible way ?
 
Last edited:
I don't understand logic of reverse string. What is logic for reverse string in c programming ?
It is not easy for me to understand exactly what you mean by the question, but I am happy to come back to it as you requested.

When you use the word 'logic' it is unclear whether you mean that you do not understand the programming logic for implementation of string reversal or whether you mean that you don't understand the justification for learning to do string reversal.

I am going to guess (in part from some of your other posts) that you mean the latter. That is, you don't understand why anyone might want to reverse strings in real life programming. You don't see how it is a problem that would be encountered often enough to justify learning to do it.

There can be situations where reversing a string is, in fact, of value - read this thread carefully for some examples https://softwareengineering.stackexchange.com/questions/24691/what-do-you-use-string-reversal-for

Some people apparently do it just because they may be asked to do it in a job interview, see here http://www.mytechinterviews.com/reverse-a-string

Beyond any practical applicability, I think it is a matter of learning to define a task and then develop and test an algorithm to solve the task. It teaches you to think about how things work in programming. Additionally, of course, you learn more about, and gain experience with using the C language.

The better you walk, the better able you will be to run - or something like that.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
The point is that to understand how an algorithm does what it does, it is necessary not only to know, for instance, that the remainder operator gives the remainder, but much more importantly, you need to know WHY that is of any value to the algorithm.
You said you can find a reverse number in many way's. I want to know how you can do this. I am asking only way not program. Would you tell me please which way you would know the reverse number.
 

WBahn

Joined Mar 31, 2012
29,843
You said you can find a reverse number in many way's. I want to know how you can do this. I am asking only way not program. Would you tell me please which way you would know the reverse number.
Okay, here's one way: Print the number to a string and then reverse the string.
 

philba

Joined Aug 17, 2017
959
Beyond any practical applicability, I think it is a matter of learning to define a task and then develop and test an algorithm to solve the task. It teaches you to think about how things work in programming. Additionally, of course, you learn more about, and gain experience with using the C language.

The better you walk, the better able you will be to run - or something like that.
I think this goes to self-reliance. The ability to take a problem, define it, dissect it, conceptualize solutions, select the best one and implement it are all part and parcel of being a capable software engineer. Inability to execute competently on each of those tasks dooms one to a lesser or non role in technology.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Okay, here's one way: Print the number to a string and then reverse the string.
Are you saying about this method of program? I have also seen some other method during the search
C:
#include <stdio.h>

int main(void)
{

    char number [10];
    char reverse[10];
  
    printf("Enter the Number = ");
  
    scanf("%s",number);

    int i=-1;
    int j=0;

          while(number[++i]!='\0');
        
           while(i >= 0)

                reverse[j++] = number[--i];
                 reverse[j] = '\0';

      printf("Reverse of  a string = %s",reverse);

      return 0;
}
Program output

Enter the Number = 786
Reverse of a string = 687
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Does it look better

C:
/*
Program  - Reverse String
Author   - Parth
Language - C Language
Date     - 09/02/2018
IDE      - GCC
*/

#include <stdio.h>

int main(void)                    
{
    char number [10];
    char reverse[10];

    printf("Enter the Number = ");

    scanf("%s",number);

    int i = -1;
    int j =  0;

          while (number[++i] != '\0');
    
           while (i >= 0)
           {
                reverse[j++] = number[--i];
             
                reverse[j] = '\0';
              
           }
               printf("Reverse of  a string = %s",reverse);

                return 0;
}
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I think this goes to self-reliance. The ability to take a problem, define it, dissect it, conceptualize solutions, select the best one and implement it.
I wrote two c program to reverse number How to find out which one is best. How do you know that the code is good
 

philba

Joined Aug 17, 2017
959
Does it look better

C:
/*
Program  - Reverse String
Author   - Parth
Language - C Language
Date     - 09/02/2018
IDE      - GCC
*/

#include <stdio.h>

int main(void)                   
{
    char number [10];
    char reverse[10];

    printf("Enter the Number = ");

    scanf("%s",number);

    int i = -1;
    int j =  0;

          while (number[++i] != '\0');
   
           while (i >= 0)
           {
                reverse[j++] = number[--i];
            
                reverse[j] = '\0';
             
           }
               printf("Reverse of  a string = %s",reverse);

                return 0;
}
Not even close. read this
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Not even close. read this
I want to know what is lacking in my code except the comment. I haven't written comment's

I tried writing a good code in my own way. I do not see anything wrong in it.

If you have few time then I would like to see your code. I want to see how you can write this code in better way
 
Top