C language programming example (medium)-sentence reverse order

C language programming example (medium)-sentence reverse order

Arrange an English sentence in reverse order in units of words. For example, "I am a boy", after the reverse order is "boy a am I",
all words are separated by a space, except for English letters, no other characters are included in the sentence

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
void IWord(char *p,int len){
    
    
	int word = 0;
	char *h = p;
	p = p + len-1;
	while (p-h>=0){
    
    
		if (*p!=' '&&p!=h){
    
    
			word++;
		}
		else if(*p==' '){
    
    
			for (int i = 1; i <= word;++i){
    
    
				printf("%c",*(p+i));
			}
			printf(" ");
			word = 0;
		}
		else if (p==h){
    
    
			for (int i = 0; i <= word; ++i){
    
    
				printf("%c", *(p + i));
			}
			printf("\n");
		}
		p--;
	}
}
int main(){
    
    
	char arr[50000];
	gets(arr);
	int len = strlen(arr);
	IWord(arr,len);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/109711639