c - 最后一封信开始

标签 c

我需要将最后一个字母放在文本中每个单词的开头,即我要输入的内容。 我写了下一个函数:

void obrabotka_file(char *fname_i, char *fname_r)
{
    FILE *in, *out;     //start and result files (streams)
    char st[RAZ];           //start string
    char sr[RAZ];           //result string
    char pr[RAZ];           //handling word
    int i, j, k;            //number of the handling word
    int n;                      // string's result length
    in = fopen(fname_i, PR_R); //open file for reading
    out = fopen(fname_r, PR_W); //open file for writing
    fgets(st, RAZ, in);     //reading string from "in" file 
    while (!feof(in))
    {
        i = 0;
        n = 0;
        sr[i] = '\0';
        while (st[i])
        {
            k = 0;
            while (st[i] == ' ')    //deleting spaces in da string
                i++;
            while (st[i] != ' ' && st[i + 1])   //select another word
            {
                pr[k+1] = st[i];
                k++;
                i++;
            }
            for (j = 0; j < k; j++, n++)
                sr[n] = pr[j];
            if (st[i])
                sr[n] = ' ';
            n++;
            i++;
        }
        sr[n++] = '\0';         //closing result string
        fprintf(out, "%s\n", sr);  //writing handled string in the new file
        fgets(st, RAZ, in);     //reading new string from the file
    }

    fclose(in); fclose(out);     //Closing "in" abd "out" files 
}

但它删除了每个单词中的第一个字母。

我的周期出了什么问题?请解释一下。

        for (j = 0; j < k; j++, n++)
            sr[n] = pr[j];
        if (st[i])
            sr[n] = ' ';
        n++;
        i++;

最佳答案

What is wrong in my cycle?

it seems like it works, but not right: … in the output i've got: Мqwert instead of yqwert

那是因为您忘记将最后一个字母放在开头,因此首字母未初始化。只需插入

            pr[0] = pr[k];

在您的周期之前。

关于c - 最后一封信开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41080925/

相关文章:

clang 链接错误 : undefined reference to 'qsort'

c - 使用指针将 double 分解为字符?

c - 为什么在 32 位系统中包含一个 int64 变量时结构大小是 8 的倍数

c - 如何使用 C 函数中的值

检查C中的输入流是否为空

c - 是否有任何 c 到字节码编译器?

c - 如何在Linux内核Makefile中添加规则

c - 在哪里可以找到信号和警报功能定义?

c - C中函数调用的默认参数

c - 预处理器的 If 条件问题