求问一道C语言的题!reverse the sentence,求给出代码
InputanEnglishsentence,reversetheorderofwordsinthesentence,buttheorderofcharactersine...
Input an English sentence, reverse the order of words in the sentence, but the order of characters in every word remains. Every word in a sentence is separated by spaces, treat punctuation as common alphabet.
Examples of the input/output of the program are as follows.
Input and Output:
I am a student.
student. a am I 展开
Examples of the input/output of the program are as follows.
Input and Output:
I am a student.
student. a am I 展开
3个回答
展开全部
#include <stdio.h>
#include <string.h>
int main()
{
char instr[100], outstr[100], stack[100];
int i, n, k, j;
printf ("Input a string : ");
gets(instr);
n = strlen(instr);
for (i = n-1, j = 0; i >= 0; i--) {
k = 0;
while (instr[i] != ' ' && i >= 0) {
stack[k++] = instr[i--];
}
k -= 1;
while (k >= 0) {
outstr[j++] = stack[k--];
}
outstr[j++] = ' ';
}
outstr[j] = '\0';
printf ("%s\n", outstr);
return 0;
}
展开全部
这题目 ... 又是送分题了 ...
用 strtok 分割句子,用 string 数组保留 word,
然后逆向输出 word ...
#include <stdio.h>
char * sentence = "I am a student";
char word_list[ 125 ][ 50 ] = { 0 };
int word_count = 0;
while( true )
{
char * token = strtok( sentence, " " );
if( ! token ) break;
strcpy( word_list[ word_count ], token );
word_count ++;
}
while( word_count )
{
printf( "%s\n", word_list[ word_count ] );
word_count --;
}
用 strtok 分割句子,用 string 数组保留 word,
然后逆向输出 word ...
#include <stdio.h>
char * sentence = "I am a student";
char word_list[ 125 ][ 50 ] = { 0 };
int word_count = 0;
while( true )
{
char * token = strtok( sentence, " " );
if( ! token ) break;
strcpy( word_list[ word_count ], token );
word_count ++;
}
while( word_count )
{
printf( "%s\n", word_list[ word_count ] );
word_count --;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,l=0,a=0,k=0;
char s[50],c[50];
gets(s);
for(i=0;s[i]!='\0';i++);
i--;
for(j=i;j>=0;j--)
{
if(s[j]!=' ')
l++;
else
{
k=1;
while(l)
{
c[a]=s[j+k];
a++;
k++;
l--;
}
c[a]=' ';
a++;
}
}
k=1;
while(l)
{
c[a]=s[j+k];
a++;
l--;k++;
}
c[a]='\0';
puts(c);
}
#include<string.h>
void main()
{
int i,j,l=0,a=0,k=0;
char s[50],c[50];
gets(s);
for(i=0;s[i]!='\0';i++);
i--;
for(j=i;j>=0;j--)
{
if(s[j]!=' ')
l++;
else
{
k=1;
while(l)
{
c[a]=s[j+k];
a++;
k++;
l--;
}
c[a]=' ';
a++;
}
}
k=1;
while(l)
{
c[a]=s[j+k];
a++;
l--;k++;
}
c[a]='\0';
puts(c);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询