反向字符串C [英] Reverse String C

查看:169
本文介绍了反向字符串C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个程序,从那是previously创建的文件中读取文本,它反转,并写入到另一个文件。在code现在我可以让我反转简单的词,但停止每当有一个空格或一个段落。我可以利用一些帮助克服这个问题。这是code我到目前为止有:

I'm trying to create a program that reads text from a file that was previously created, inverts it, and writes to another file. The code I have now allows me to invert single words, but stops whenever there is a space or a paragraph. I could use some help in overcoming this problem. This is the code I have so far:

#include <stdio.h>
#include <string.h>

int main()
{
  int c, d;
  FILE *fich1, *fich2;
  char string [100], *inicio, *fim, tempo;

  fich1 = fopen("FicheiroInicial.txt", "rt");
  if (fich1 == NULL)
    {
      printf ("Erro - Nao foi possivel abrir o ficheiro\n");
      return (-1);
    }
  fscanf (fich1, "%s", string);
  fclose (fich1);
  inicio = string;
  fim = inicio + strlen(string) - 1;

  while (fim > inicio)
    {
      tempo = *inicio;
      *inicio = *fim;
      *fim = tempo;

      ++inicio;
      --fim;
    }  

  fich2 = fopen("FicheiroFinal.txt", "wt");
  fprintf (fich2, "%s", string);
  fclose (fich2);
  printf ("%s", string);

  return 0;
}

在此先感谢!

推荐答案

我相信fscanf()函数只会给你一次一个字。你需要遍历它,直到它返回EOF(文件结束)

I believe fscanf() will only give you a word at a time. you will need to iterate over it until it returns EOF (end of file)

这篇关于反向字符串C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆