This question already has answers here:
Reversing string in C
(3个答案)
5年前关闭。
我很乐意为自己完成的任务提供帮助,但似乎无法获得帮助。
我需要编写一个函数来更改句子中的字符顺序。例:
“ hello world”将变成:“ olleh dlrow”。
这是我得到的功能原型:
我不能使用指针,静态var或其他任何东西。
每个字之后都有一个空格,我该怎么办?
并且该函数需要返回一个值..它是什么?我不明白,因为该功能只是改变字符的顺序。
这是我写的:
谢谢 :)
(3个答案)
5年前关闭。
我很乐意为自己完成的任务提供帮助,但似乎无法获得帮助。
我需要编写一个函数来更改句子中的字符顺序。例:
“ hello world”将变成:“ olleh dlrow”。
这是我得到的功能原型:
int changeCharOrderInSentence(char table[][MAX_SENTENCE_LENGTH], int numOfSentences, int sentenceToChange)
我不能使用指针,静态var或其他任何东西。
每个字之后都有一个空格,我该怎么办?
并且该函数需要返回一个值..它是什么?我不明白,因为该功能只是改变字符的顺序。
这是我写的:
int changeCharOrderInSentence(char table[][MAX_SENTENCE_LENGTH], int numOfSentences, int sentenceToChange)
{
int i,j,lensentence;
char temp;
lensentence=strlen(table[sentenceToChange]);
while(table[sentenceToChange])// as long as we are not at the end of the chosen sentence-'\0'
{
for(i=sentenceToChange;(lensentence)/2;i++)
{
temp=table[sentenceToChange][i];
table[sentenceToChange][i]=table[sentenceToChange][lensentence-1-i];
table[sentenceToChange][lensentence-1-i]= temp;
}
}
}
谢谢 :)
最佳答案
使用每个单词都以空格结尾的事实来查找每个单词,然后对每个单词使用反向代码。
关于c - C程序-更改句子中的字符顺序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27217248/