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.

Recommended Answers

All 2 Replies

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.

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

printf("%s\n,name_of_string)

commented: And how does this comment help with printing the string backwards? -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.