Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys i am trying to make c program for reversing the string by returning the pointer to char from the function

What I have tried:

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

char* reversestring(char* input);
int main()
{
	int i;
	char* output;
	char inputstring[100];
	printf("enter the string\n");
	gets(inputstring);
	output = reversestring(inputstring);

i want to print the same string in both way that is by using printf and by running the loop please suggest me both way to print in same program

here i have taken i=0 to 4 for fixed string here i m assuming that user will enter a fixed string of length 5 char because i dont know the proper way
	printf("%s\n",output);
	for(i=0;i<4;i++) {
		printf("%c",(output+i));
	}
}


reverse function start from here
char* reversestring(char* input)
{
    int count=0,begin,end;

	char* output=(char*) malloc(100);
	while(*input!=0) {
		count++;
	}

	end=count-1;

	for(begin=0;begin<count;begin++) {
		*(output+begin)=*(input+end);
		 end--;
	}

	output[begin]='\0';
	return output;
}

there is no error and warning it is taking input only not giving the output(cursor is blinking only program is not ending)
Posted
Updated 11-Jun-18 3:05am
v2

This loop won't finish:
while(*input!=0) {
    count++;
}


You could replace it with:
while (*(input + count) != 0) {
    count++;
}


To print your reversed string character by character, use strlen[^]:
for (int i = 0; i<strlen(output); i++) {
    printf("%c", *(output + i));
}
 
Share this answer
 
Comments
CPallini 11-Jun-18 9:12am    
for (int i = 0; i<strlen(output); i++)
is quadratic on string length
[no name] 11-Jun-18 9:15am    
That is a fair point. Since we're dealing with short strings here I went for the short/simple form, but for performance reasons it makes sense to put it in a separate variable first.
CPallini 11-Jun-18 12:07pm    
My 5.
In my opinion the memory allocation should be caller responsibility. Moreover, never ever use gets function.
Try
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * reverse( char * rev, const char *  str, size_t length )
{
  size_t n;
  for (n=0; n<length; ++n)
  {
    rev[n] = str[length-1-n];
  }
  rev[n] ='\0';
  return rev;
}

int main()
{
  char s[100];
  printf("please enter the string:\n");

  if ( ! fgets( s, sizeof(s), stdin) ) return -1;

  size_t length = strlen(s);

  char * r = (char *) malloc( length+1 );
  if ( ! r ) return -2;

  reverse( r, s, length);

  printf("reversed: %s\n", r);

  free(r);
  return 0;
}
 
Share this answer
 
Comments
[no name] 11-Jun-18 9:17am    
5 - I totally agree with memory allocation being the caller responsibility, but given the previous question I figured that's not what he wanted. https://www.codeproject.com/Questions/1247845/Program-to-reverse-a-string-using-pointers-in-C
CPallini 11-Jun-18 12:07pm    
Thank you very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900