Program not running

Oct 31, 2015 at 7:30pm
hi,
I am trying to make a program that reverses a string. example: input:hello, output:olleh
the program is given below but for some reason its not running.Compilation took place and there are 0 errors but nothing happens adter we give the input Can someone please help me in this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace std;
#include<iostream>
#include<stdio.h>
#include<string.h>
int main()
{
	char A[50],hold;
	int len,i,j;
	cout<<"Enter the string "<<endl;
	gets(A);
	len=strlen(A);
	for(i=0,j=(len-1);i<(len-1),j>=0;i++,j--)
	{
		while(j>=i)
		{
			hold=A[i];
			A[i]=A[j];
			A[j]=hold;
		}
	}
	puts(A);
} 
Last edited on Nov 1, 2015 at 7:24am
Oct 31, 2015 at 9:37pm
Oct 31, 2015 at 11:01pm
1
2
3
4
5
6
while(j>i)
{
   hold=A[i];
   A[i]=A[j];
   A[j]=hold;
}
¿how many times would that loop execute?


Also
man gets wrote:
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
Last edited on Oct 31, 2015 at 11:02pm
Oct 31, 2015 at 11:14pm
closed account (E0p9LyTq)
Your program does run, your while() statement creates an infinite loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

int main()
{
   char hold;
   std::string A;
   int i = 0;
   int j = 0;

   std::cout<<"Enter the string: ";
   std::getline(std::cin, A);

   for (i = 0, j = A.size() - 1; i < A.size() - 1, j >= 0; i++,j--)
   {
      if (j > i)
      {
         hold = A[i];
         A[i] = A[j];
         A[j] = hold;
      }
   }
   std::cout << A << std::endl;
   
   return 0;
}
Nov 1, 2015 at 7:34am
Ok...got it
The while loop is infinite...Had to use if statement instead of while. Thanks to all :D
Topic archived. No new replies allowed.