ALLOCATE SPACE FOR A STRING DYNAMICALLY AND
PRINT THE STRING BACKWARDS
Aim
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.
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 <stdlib.h>
#include <stdio.h>
#inciude <string.h>
mt main(void)
{
char *S;
register int i;
register int i;
s = malloc(80);
if(!s) {
printf(”Memory request failed.\n”);
exit(1);
printf(”Memory request failed.\n”);
exit(1);
}
gets(s);
for(i = strien(s) - 1; i >= 0; i--) putchar(s[ i ]);
for(i = strien(s) - 1; i >= 0; i--) putchar(s[ i ]);
free(s);
return 0;
}
return 0;
}
Output:
Hai
iaH
Result:
Thus the program is written and executed to allocate space for a string dynamically and print the string backwards.
0 comments:
Post a Comment