C++ 使用指针按单词反转字符串
下面我的程序不是按字符而是按单词反转字符串 样本输入你好,梅洛 Sanple输出Hello Mello Hello 我不知道第一声问候是从哪里来的C++ 使用指针按单词反转字符串,c++,pointers,C++,Pointers,下面我的程序不是按字符而是按单词反转字符串 样本输入你好,梅洛 Sanple输出Hello Mello Hello 我不知道第一声问候是从哪里来的 #include "stdafx.h" #include <iostream> int main(int argc, char **argv) { char input[255]; char *head; // head of a word. char *tail; // tail of a word. printf(
#include "stdafx.h"
#include <iostream>
int main(int argc, char **argv)
{
char input[255];
char *head; // head of a word.
char *tail; // tail of a word.
printf("Enter a sentence: ");
gets_s(input); // read a whole line.
tail = input + strlen(input) - 1;
while (tail >= input)
{
if (*tail == 0 || *tail == ' ')
{
tail--; // move to the tail of a word.
}
else
{
tail[1] = 0;
head = tail;
while (head >= input)
{
if (head == input || *(head - 1) == ' ')
{
printf("%s",input); // output a word.
printf(" ");
tail = head - 1; // seek the next word.
break;
}
head--; // move to the head of a word.
}
}
}
printf("\n\n");
system("pause");
return 0;
}
#包括“stdafx.h”
#包括
int main(int argc,字符**argv)
{
字符输入[255];
char*head;//单词的开头。
char*tail;//单词的尾部。
printf(“输入一个句子:”);
获取_s(输入);//读取整行。
尾部=输入+strlen(输入)-1;
while(tail>=输入)
{
如果(*tail==0 | |*tail==“”)
{
tail--;//移到单词的末尾。
}
其他的
{
尾[1]=0;
头=尾;
while(头>=输入)
{
if(head==输入| |*(head-1)='')
{
printf(“%s”,输入);//输出一个单词。
printf(“”);
tail=head-1;//查找下一个单词。
打破
}
head--;//移到单词的开头。
}
}
}
printf(“\n\n”);
系统(“暂停”);
返回0;
}
有人能帮忙吗?我想你的意思是打印头,而不是输入。您从字符串开始打印,而不是从刚找到的单词开始打印
printf("%s",head); // output a word.
我想你的意思是打印头,不是输入。您从字符串开始打印,而不是从刚找到的单词开始打印
printf("%s",head); // output a word.
是的,它必须是相当的C-ISH C++…不允许使用字符串。是的,它必须相当于C-ISH C++。不允许使用字符串。谢谢,这非常有效,现在我的大脑可以放心:)(我会在计时器后将其标记为正确的)我现在明白它为什么打印刚刚找到的单词。谢谢,这非常有效,现在我的大脑可以放心:)(我会在计时器后将其标记为正确的)我现在明白它为什么打印刚刚找到的单词。