how to reverse it?
i had tried

#include <stdio.h>
void main()
{
	int i = 30;
	int x;
	char reverse[31];
	char title[31] = "programming concepts and design";

	for(x = 0; x >= 0; x++)
	{
		reverse[x] = title[i];
		i--;
	}

	printf("The reverse order is %s\n", reverse[x]);
}

the result show me is the system error and end of the program immediately.
so what's wrong with the codes, so far i can't detect where im wrong.

Recommended Answers

All 6 Replies

Problem occurs within your loop-condition. It will continue as long as x>=0, since the it always fulfill it won't stop.

Your for loop has a bad test to end the loop. x will always be greater or equal to zero, so the loop quickly is working outside the array.

for(x=0;x<strlen(title);x++)

should work.

It is more efficient to first, before the for loop, get the length of the string assigned to an int variable, then just use:

len=strlen(title);
for(x=0;x<len;x++) 
  //rest of code here

because that saves the program from having to measure the length of the string, every time through the loop.

Try to not use <31 or any other inflexible numbers in your code, if possible. Make it so it works with a char array of any length, see?

oh, i got it.
thanks everyone for helping (:

Hellloo :

I want to ask , what about integers , if there is these integers 12 23 58 93 how you can revrse them to 93 58 23 12 ??

THx :)

Same basic logic. Just change the test in the top of the for loop - somehow, your testing condition has to know when it has reached the end of the numbers. You could have an ending sentinel value, or just use the number of integers that you have in the array.

Work with this, and post up your code if you're still stumped. You can do this.

Here's my two cents...I used the existing variable to reverse the characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* reverseit(char* str, unsigned long size)
{
	unsigned long i = 0;
	unsigned char c;

	for (i = 0; i < (size / 2); ++i)
	{
		c = str[(size - 1)- i];
		str[(size - 1) - i] = str[i];
		str[i] = c;
	}

	return str;
}

int main(int argc, char**argv)
{
	if (argv[1])
		fprintf(stdout, "%s\n", reverseit(argv[1], strlen(argv[1])));
	
	exit(EXIT_SUCCESS);
}
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.