#include <stdio.h>
void rev(char *pstr);
void swap(char *bg,char *en);
int main(){
char str[]="hii my name is adarsh";
rev(str);
printf("%s",str);
}
void rev(char *pstr){
char *big=NULL;
char *end=pstr;
//printf("...%s...",pstr);
while(*end){
if((big==NULL) && (*end!=' ')){ //string should not be null or end with ' '
big=end;
}
if((big && ((*(end+1)==' ') || (*(end+1)=='\0')))){//string last ' ' or '\0'
swap(big,end);
big=NULL;
}
end++;// char count 1 by one// string counting and iterating
}
swap(pstr,end-1);
}
void swap(char *bg,char *en){
char temp;
while(bg<en){
temp=*bg;
*bg++=*en;
*en--=temp;
}
}
/* % use
The symbol “%” denotes the beginning of a format mark. The mark “%d” is replaced
by the value of variable counter and the resulting string is printed.
The symbol “\n” represents a new line. By default, all output is right-justified,
that is, is placed on the right edge of the field and, by default,
the width of the field is the same width as the string length.
Examples of format marks
The format marks included as part of the first parameter passed to printf
offer many possibilities. It follows a description of some examples:
Specify the minimum field width: The C language allows you to add an integer
between the percent sign (%) and the letter in a format specifier. This ensures
that the output reaches the minimum width. For example, %10f ensures that the
output is at least 10 character spaces wide. This is especially useful when
printing out a column of values. For example, with the next code:
int num = 12;
int num2 = 12345;
printf("%d\n",num2);
printf("%5d\n",num);
the following output is printed:
12345
12
Align output: By default, all output is right-justified when using the minimum
field width. You can change this and force output to be left- justified.
To do so, you need to prefix the minimum field specifier with the minus sign (-).
For example, %-12d specifies the minimum field width as 12, and justifies the
output from the left edge of the field.
Specify precision: You can put a period . and an integer right after the minimum
field width specifier to have a precision specifier. You can use the precision
specifier to determine the number of decimal places for floating-point numbers,
or to specify the maximum field width (or length) for integers or strings.
# used for
When we include library or any macro or other piece of code which needs
preprocessing,it is used to distinguish lines which need preprocessing before
actual compilation begins..
So we include # for compiler to know which lines needs preprocessing..
As others have already pointed out, # is a pre-processor directive.
It is used to distinguish lines which need preprocessing before actual
compilation begins.
Any line which begins with # is replaced/omitted with other piece
of code and then that code is compiled. For example when preprocessor encounters
a line
# include <stdio.h>
it replaces that line with all the text inside 'stdio.h'.
Similarly for macros like
#define ADD(a,b) ((a)+(b))
it replaces all instances of ADD with definition of ADD.
So # is used by C compiler to know which lines need preprocessing.
As in why '#' and not some other character, I guess all other characters
are already used for some operation/functionality.
*/
#if 0
#include<stdio.h>
//function prototype to reverse
//the string from begin to end
void revString(char *pBegin, char *pEnd)
{
char temp;
while (pBegin < pEnd)
{
temp = *pBegin;
*pBegin++ = *pEnd;
*pEnd-- = temp;
}
}
// Function to reverse words
void revWord(char *pString)
{
// store the beginning address of word
char *word_begin = NULL;
//word_boundary is for word boundary
char *word_boundary = pString; /* */
//Loop to reverse the character of words
while( *word_boundary )
{
//This condition is to make sure that
//the string start with valid character
if (( word_begin == NULL ) && (*word_boundary != ' ') )
{
word_begin = word_boundary;
}
if(word_begin && ((*(word_boundary+1) == ' ') || (*(word_boundary+1) == '\0')))
{
revString(word_begin, word_boundary);
word_begin = NULL;
}
word_boundary++;
}
// reverse the whole string
revString(pString, word_boundary-1);
}
int main()
{
//source string
char src[] = "How are you";
/*
char src[]; //this will not work
scanf("%s",src);
*/
//Reverse the word of the string
revWord(src);
//print the resultant string
printf("Resultant string = %s", src);
return 0;
}
#endif
To embed this program on your website, copy the following code and paste it into your website's HTML: