ALLOCATE SPACE FOR A STRING DYNAMICALLY AND PRINT THE STRING BACKWARDS - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Wednesday, October 27, 2010

ALLOCATE SPACE FOR A STRING DYNAMICALLY AND PRINT THE STRING BACKWARDS

To write a program in c to allocate space for a string dynamically and print the string backwards

Algorithm:
1.Start the program.
2.Declare the pointer variable s.
3.Allocate the memory space using malloc.
4.Ifs = = NULL, allocation is not done. Else done successfully.
5.Get the string from the user
6.Read the string from backward and print the result.
7.Stop the program.

Program
#include
#include
#inciude
mt main(void)
{
char *S;
register int i;
s = malloc(80);
if(!s) {
printf(”Memory request failed.\n”);
exit(1);
}
gets(s);
for(i = strien(s) - 1; i >= 0; i--) putchar(s[ i ]);
free(s);
return 0;
}

Output:
Hai
iaH

No comments:

Post a Comment