Monday, July 27, 2009

Code in c to print string (line) in reverse order?

This is a c Program





Program c a is this

Code in c to print string (line) in reverse order?
#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;


main()


{


char str[100], rstr[100];


int i,k;


printf("Enter the string to reverse=%26gt;");


scanf("%s",str);


for (i=strlen(str)-1,k=0; i%26gt;=0; i--,k++)


{


rstr[k]=str[i];


}


rstr[k]='\0';


printf("string %26lt;%s%26gt; reverse %26lt;%s%26gt;\n",str,rstr);


}
Reply:I wrote one yesterday:


#include "stdio.h"


#include "stdlib.h"


int main(){


char* s = malloc(1);


int c = 0;


int buf;


*s = getchar();


int x = 1;


while(x){


while(s[c] != '\n'){


c++;


s = realloc(s,c + 1);


buf = getchar();


if(buf == EOF)exit(0);


s[c] = buf;


}/*this gives a string, terminated by a newline*/


c--;


while(c != -1){


putchar(s[c]);


c--;


}


putchar('\n');


}


free(s);


}


my solution, unlike the one above, has no limits on line size. the programmer above me has C programmer's disease. Sir, is it THAT hard to use dynamic allocation?


No comments:

Post a Comment