Write a C Program to Reverse each word in string using pointers. Here’s simple Program to Reverse each word in string using pointers in C Programming Language.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
The unary or monadic operator & gives the “address of a variable’”.
The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.
Below is the source code for C Program to Reverse each word in string using pointers which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to Reverse each word in string using pointers */
#include <stdio.h>
#include <conio.h>
int main()
{
char str[50];
int len,i,j,sp=0,start,end;
char *s;
printf("Enter any String :: ");
gets(str);
s=str;
printf("\nAfter reversing Each Word of String [ %s ] :: \n",s);
len=strlen(s);
end=len-1;
for(i=len-1;i>=0;i--)
{
if(s[i]==32 || i==0)
{
if(i==0)
start=0;
else
start=i+1;
for(j=start;j<=end;j++)
printf("%c",s[j]);
end=i-1;
printf(" ");
}
}
printf("\n");
return 0;
}
Output : :
/* C Program to Reverse each word in string using pointers */
Enter any String :: CodezClub is a Programming Website
After reversing Each Word of String [ CodezClub is a Programming Website ] ::
Website Programming a is CodezClub
Process returned 0
Above is the source code for C Program to Reverse each word in string using pointers which is successfully compiled and run on Windows System.The Output of the program is shown above .
What are Pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of a pointer variable declaration is −
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
The unary or monadic operator & gives the “address of a variable’”.
The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.
Below is the source code for C Program to Reverse each word in string using pointers which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
Output : :
Above is the source code for C Program to Reverse each word in string using pointers which is successfully compiled and run on Windows System.The Output of the program is shown above .
need an explanation for this answer? contact us directly to get an explanation for this answer