I don't understand how words in the sentence is reversed, using the next code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char *sent=(char*)calloc(120, sizeof(char*));
puts("enter sentence:");
gets(sent);
size_t n= strlen(sent);
char arr[n+1];
char *p = arr;
char *q = sent + n;
while (q != sent){
while (q != sent && isblank(*(q-1))) *p++ = *--q;
char *r = q;
while (r != sent && !isblank(*(r-1))) --r;
memcpy(p, r, q-r);
p+= q - r;
q = r;
}
puts("original sentence:");
puts(sent);
puts("reversed sentence:");
puts(arr);
return 0;
}
May somebody explain me what pointers *q
and *r
mean, please?
both, q and r, point to the end.
While q serves to process spaces (to point to the end of the word)
r is used to detect the beginning of a word.
Resp. memcpy(p, r, qr)
takes a word from r of length qr and places it in p
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.