扭转的话顺序在C句子 [英] reverse order of words in a sentence in c
问题描述
我想写code,但我无法找到任何有关我的任务是正确的。这是令人沮丧的。我需要的程序,读取从行input.txt的
文件和文字的相反顺序写入 output.txt的
文件。
input.txt的
可能会改变,所以我们不知道有多少的线条和文字也有在里面。所有标点符号必须清除。如果有字与字之间不止一个空格,这个程序应该给我们 output.txt的
与单词之间的空格。
例如:
input.txt的
这是一本书吗?是的,是的。
块引用>output.txt的
的sI SIHT一个koob?赛克,踢死。
块引用>解决方案下面是做这项工作的一个示例程序:
的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;/ *函数来获取所传递的字符串的反向* /
void反转(字符*串,FILE * FP)
{
如果(*字符串)
{
反向(字符串+ 1,FP);
的fputc(*字符串,FP);
}}
诠释的main()
{
字符字[100];
为size_t LEN = 0;
FILE * fpInput,* fpOutput;
fpInput = FOPEN(input.txt的,R +);
fpOutput = FOPEN(output.txt的,W +);
而(的fscanf(fpInput,%98S,字)大于0)
{
strncat函数(字,,1);
反向(文字,fpOutput);
}
返回0;
}I am trying to write code but i can't find anything correct about my task. This is frustrating. I need program that reads line from
input.txt
file and writes the reverse order of words tooutput.txt
file.
input.txt
may change, so we don't know how many lines and words there are in it. All punctuation marks must be removed. If there are more than one space between words, this program should give usoutput.txt
with single space between the words.For example:
input.txt
Is this a book? Yes,It is.
output.txt
sI siht a koob? seY, tI si.
解决方案Here's a sample program that does the job:
#include <stdlib.h> #include <stdio.h> /* Function to get reverse of the passed string */ void reverse(char *string, FILE* fp) { if(*string) { reverse(string+1,fp); fputc(*string,fp); } } int main() { char word[100]; size_t len = 0; FILE * fpInput,*fpOutput; fpInput = fopen ("input.txt", "r+"); fpOutput = fopen ("output.txt", "w+"); while(fscanf(fpInput,"%98s",word)>0) { strncat(word," ",1); reverse(word,fpOutput); } return 0; }
这篇关于扭转的话顺序在C句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!