#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
// function prototype declarations
int length(char a[20]);
void main()
{
int count=0,i;
int len; // length of the string
char c;
char x[20]; // array of characters. string
// get a character
printf("\n Enter a word and press <enter>\n");
c=getchar();
while ( c!='\n') // ‘\n’ is end of line character i.e. pressing enter key
{
x[count]=c;
count++;
c=getchar();
}
// we have reached end of line. Append ‘\0’ to the string
x[count]= '\0';
len = count;
// Now display the string you have just read
printf("\n String inputted : %s ", x);
printf("\n space allocated to single char : %d byte", sizeof(char));
printf("\n Memory space allocated to string x[] : %d ", sizeof(x));
printf("\n No of characters in the string x [] : %d ", length (x));
// now reverse the string
printf("\n string X reversed.\n");
for ( i=len-1;i>=0;i--)
printf("%c",x[i]);
printf("\n");
system("pause");
}
int length(char a[]) /*function definition*/
{
int i=0;
while(a[i]!='\0') /*when the character is not null*/
i++;
return i;
}/*end of function length*/
hasilnya
No comments:
Post a Comment