C 语言中的字符串处理问题



>我被困在一个硬件作业上,我需要编写一个程序,将一堆英语单词(在输入.txt文件中由新行分隔的列表中(转换为一堆 Pig Latin 单词(转换为输出.txt文件中由新行分隔的列表(。我已经非常接近了,但是我正在使用的strncat函数(字符串协调(函数以某种方式抛出一个新行,这确实抛弃了我正在打印到stdout的文本(现在使用它进行测试(。知道为什么会发生这种情况吗?这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_SIZE 100
char * convertToPigLatin (char * strPtr, char * pLatinStr);
int main(int argc, char *argv[])
{
   char str[MAX_STR_SIZE];
   char pStr[MAX_STR_SIZE];
   //char *pStrPtr; 
   FILE *fileInPtr;                                     //Create file name
   FILE *fileOutPtr;    
   fileInPtr = fopen("pigLatinIn.txt", "r");    //Assign text to file
   fileOutPtr = fopen("pigLatinOut.txt", "w");
   //pStrPtr = pStr; 
   if(fileInPtr == NULL)                                //Check if file exists
   {
      printf("Failed");
      exit(-1); 
   }

   do                   //Cycles until end of text
   {
      fgets(str, 29, fileInPtr);                //Assigns word to *char
      str[29] = '';                           //Optional: Whole line
      convertToPigLatin(str, pStr); 
      fprintf(fileOutPtr, "%s", pStr); 
   }  while(!feof(fileInPtr));   
   system("pause"); 
}
char * convertToPigLatin (const char * strPtr, char * pStrPtr)
{
   int VowelDetect = 0; 
   int LoopCounter = 0; 
   int consonantCounter = 0; 
   char pStr[MAX_STR_SIZE] = {''};
   char cStr[MAX_STR_SIZE] = {''};
   char dStr[] = {'-',''}; 
   char ayStr[] = {'a','y',''};
   char wayStr[] = {'w','a','y',''};
   pStrPtr = pStr; 
   while (*strPtr != '')
   {
      if (*strPtr == 'a' || *strPtr == 'e' || *strPtr == 'i' || *strPtr == 'o' || *strPtr == 'u' || VowelDetect ==1)
      {
         strncat(pStr, strPtr, 1); 
         VowelDetect = 1; 
      }
      else
      {
         strncat(cStr, strPtr, 1); 
         consonantCounter++; 
      }
      *strPtr++;
   }
   strcat(pStr, dStr); 
   if (consonantCounter == 0)  
   {
      strcat(pStr, wayStr);
   }
   else
   {
      strcat(pStr, cStr);
      strcat(pStr, ayStr);
   }  
   printf("%s", pStr);                       
 //  return pStrPtr; 
}

这段代码中有很多奇怪之处,但是您询问的问题是由convertToPigLatin中的while循环创建的。你在*strPtr != ''时循环,n肯定不是,所以你把它添加到pStr

现在对于其余代码,只有一些注释:

  • 使用 strncat 复制一个字符是奇怪的用法。通常,人们会使用简单的字符分配来代替(如str1[i] = str2[j]
  • (
  • *strPtr++递增指针并取消引用它,但随后不对取消引用的值执行任何操作。你只是想strPtr++在那里。
  • 您可以使用 char str[] = "some string" 创建字符串文本。不需要使用数组初始化语法。

这些是那些在没有详细阅读的情况下跳出来给我的。祝你未来的任务好运。

编辑以添加,在这些情况下,使用调试器单步执行代码非常有价值。您将确切地看到添加换行符的位置。

问题不在于 strncat,而在于您的输入:

fgets(str, 29, fileInPtr);                //Assigns word to *char
str[29] = '';                           //Optional: Whole line

如果输入 29 个字符以外的内容,则不会覆盖硬回车。请改用这个:

str[strlen(str)-1] = 0;

.. 实际上,这将始终覆盖第 29 个字符,即使它不是硬回车(当您输入超过 29 个字符时(。所以更好的解决方案是

char *ptr = strchr(str, 'n');
if (ptr) *ptr = 0;

您也不能为此使用MAX_STR_SIZE(您定义了 - 但没有在代码中使用(,因为 fgets

[r]eads流中的字符并将它们作为C字符串存储到str中,直到读取(num-1(字符或到达换行符或文件末尾,以先发生者为准。(http://www.cplusplus.com/reference/cstdio/fgets/(

-- 最后一个字符可以是终止零,也可以是硬回车。

@selbie是对的,

*strPtr++;

==>

++strPtr;

在调用转换到猪拉丁语之前也添加这个

if(str[0] == '')
        break;    

它有效,猪拉丁出来.txt用拉丁语显示工作,但都在一行!

像这样:(我对拉丁语一无所知,这是你想要的吗?

猪拉丁语.txt

hello world!
I am line one!
I am line two!
I am line three!

猪拉丁.txt

𬌿q·ð¬Œ¿q·ð¬Œ¿q·ð¬Œ¿q·

最新更新