Thanks for Your tip @laserlight! So run it correctly - almost. Where does the droll signs come from?
Code:
//Reverse a sentence by "mh2x", and "strrev" reverse it
//than word by word. For Windows only. - Oktober 2018
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *ReverseChars(char *pStart, char *pEnd);
char *ReverseSentence(char *pSentence);
int main(int argc, char *argv[])
{
if (argc < 2) return 1;
char *dateiname = argv[1];
FILE *dz = fopen(dateiname, "r");
if (dz == NULL) return 1;
char *sentence;
char c;
int z = 0;
while((c = fgetc(dz)) != EOF)
{
sentence[z++] = c;
}
fclose(dz);
for(int i = 0; i < z; i++)
putchar(sentence[i]);
printf("\n\n");
ReverseSentence(sentence);
printf("%s", strrev(sentence));
printf("\n\n");
return 0;
}
char *ReverseChars(char *pStart, char *pEnd)
{
if (NULL == pStart || NULL == pEnd)
return NULL;
char *pString = pStart;
while (pEnd > pStart)
{
char temp = *pStart;
*pStart = *pEnd;
*pEnd = temp;
pStart++;
pEnd--;
}
return pString;
}
char *ReverseSentence(char *pSentence)
{
if (NULL == pSentence)
return NULL;
char *pStart = pSentence;
char *pEnd = pSentence;
while (*pEnd)
{
if (*pEnd == ' ')
{
//Reverse a word
ReverseChars(pStart, pEnd - 1);
pStart = pEnd + 1;
}
pEnd++;
}
//Revese last word
ReverseChars(pStart, pEnd-1);
//Reverse the whole sentence
ReverseChars(pSentence, pEnd - 1);
return pSentence;
}