0

Hi guys here is what I aim to do :
Create a program which
1. reads in a string of less than 25 characters from the user
2. prints out the string followed by its reverse
i.e. if the user enters "hello", the program should print "helloolleh".

I can do parts 1 and some of part 2 however on my output it only prints "olleh", and It has some other weird box split into 4 with 1 and 0's over the o.
Firstly how do i stop the weird box.
Secondly how do I go about printing the string out and then its reverse:
here is my code:

#include <stdio.h>
#include "simpleio.h"

char array1[26];
int i;
char array2[26];

int main()
{
	printf("Please enter string (of 25 characters length): ");
	getString(array1,25);
		
	for(i=array1[i]; i>=0; i--)
	{		
	 printf("%c",array1[i]);
	}	
	printf("\n");
 return 0;
}

Thanks. Please quote when replying.

Edited by Don_k: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by abhimanipal
0

you will have to initialize i in your for loop to the length of the string entered. To get the length of the string entered, use the strlen() function. for (i=strlen(array1)-1; i>=0; i--) If you have something else defined in your simpleio.h that returns the length of the string you can use that too.

Edited by shah1248: n/a

-1

For printing the string the normal way (unreversed) you can use

printf("%s\n,name_of_string)

Votes + Comments
And how does this comment help with printing the string backwards?
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.