C String Reversal without Recursion or Built in Functions

can not use recursive or built in functions

Page 1 of 1

4 Replies - 13016 Views - Last Post: 25 November 2009 - 08:53 PM Rate Topic: -----

#1 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

C String Reversal without Recursion or Built in Functions

Post icon  Posted 25 November 2009 - 06:17 PM

I need to write a program that takes a line of text from the keyboard and reverses it so that it is displayed backwards.
Like this: C is fun! !nuf is C

Here is what I have so far:

#include <stdio.h>

void reverse(const char * const sPtr);
 
int main(void) { 
 
  // Local Variables
  char sentence[80];

  // Begin
  printf("Enter a line of text:\n");
  gets(sentence);

  printf("\nThe line reversed is:\n");
  reverse(sentence);


  return 0;
} //end function main

void reverse(const char * const sPtr){

	while(sPtr[0]!='\0'){
		putchar(sPtr[0]);
		break;
	}
} //end function reverse




Right now it enters an infinite loop without the break, but it at least prints the first character. All I need help with is getting the reversal function to print the input backwards, the main function works just fine.

Is This A Good Question/Topic? 0
  • +

Replies To: C String Reversal without Recursion or Built in Functions

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: C String Reversal without Recursion or Built in Functions

Posted 25 November 2009 - 06:27 PM

Do you have to store it? You could just iterate backwards.

void reverse(const char* ptr){
	int length = strlen(ptr);
	for(int i = length - 1; i >= 0; i--){
			printf("%c", ptr[i]);
	}
	return;
}
 
int main() {
	reverse("Knowles is awesome!");
	return 0;
}




What is your professor's a definition of a built in function?
Was This Post Helpful? 1
  • +
  • -

#3 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: C String Reversal without Recursion or Built in Functions

Posted 25 November 2009 - 07:46 PM

the strrev() function and any other function like it are considered built in. It has to be a function that reverses the order without recursion or built in functions that reverse the order. I know loop control will do it, but I'm having trouble getting the proper syntax for the proper loop control. I know it will either use a For loop or a While loop. That's about it. The While loop that is show here enters an infinite loop without the break. The For loop I can use, but the only way that I can find to use the For loop uses recursive functions. I will give your code a try, but as I said before, the main function works fine, it takes the keyboard input and stores it as a string, then passes that string to the reverse function, which alters it and returns it to the main function reversed to be printed to the screen. So, the only function I need help with is the reverse function.

View PostKYA, on 25 Nov, 2009 - 05:27 PM, said:

Do you have to store it? You could just iterate backwards.

void reverse(const char* ptr){
	int length = strlen(ptr);
	for(int i = length - 1; i >= 0; i--){
			printf("%c", ptr[i]);
	}
	return;
}
 
int main() {
	reverse("Knowles is awesome!");
	return 0;
}




What is your professor's a definition of a built in function?

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5907
  • View blog
  • Posts: 12,811
  • Joined: 16-October 07

Re: C String Reversal without Recursion or Built in Functions

Posted 25 November 2009 - 07:57 PM

Well, the big thing is, this isn't stopping because it's not moving.

See comments:
void reverse(const char * const sPtr){
	int i = 0; // make an iterator
	while(sPtr[i]!='\0'){
		putchar(sPtr[i]);
		i++; // interate
	}
}



Of course, that's not reversing anything.

If you're not allowed to use strlen, you can just go up then down:
void reverse(const char * const sPtr){
	int i = 0;
	while(sPtr[i]!=0) { i++; }
	while(--i>=0) { putchar(sPtr[i]); }
}


Was This Post Helpful? 1
  • +
  • -

#5 ThePheonix21  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 25-November 09

Re: C String Reversal without Recursion or Built in Functions

Posted 25 November 2009 - 08:53 PM

Either of those look like they should work. I ran into something like this on a test, except that the task was to write a function that took integer a to the nth power. We weren't allowed to use the pow(), so I used the exp() function, I was then told that we couldn't use that. So I pretty much did the same thing you did in those functions, except writing it so it took the integer to the nth power.

View Postbaavgai, on 25 Nov, 2009 - 06:57 PM, said:

Well, the big thing is, this isn't stopping because it's not moving.

See comments:
void reverse(const char * const sPtr){
	int i = 0; // make an iterator
	while(sPtr[i]!='\0'){
		putchar(sPtr[i]);
		i++; // interate
	}
}



Of course, that's not reversing anything.

If you're not allowed to use strlen, you can just go up then down:
void reverse(const char * const sPtr){
	int i = 0;
	while(sPtr[i]!=0) { i++; }
	while(--i>=0) { putchar(sPtr[i]); }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1