所以我的代码成功地将英语转换为猪拉丁语,但是当我将变量传递回 main{} 时,我只能以某种方式返回地址位置或一些十六进制或字符数。我尝试使用不同的转换说明符和所有内容,但由于某种原因,我无法让它输出字符串。该程序从输入 txt 文件中读取,计算翻译,然后打印到输出 txt 文件。让我知道您可能认为是什么问题。谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_SIZE 50
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");
if(fileInPtr == NULL) //Check if file exists
{
printf("Failed");
exit(-1);
}
fprintf(fileOutPtr, "English Word\t\t\t\tPig Latin Word\n", pStr);
fprintf(fileOutPtr, "---------------\t\t\t\t----------------\n", pStr);
do //Cycles until end of text
{
fscanf(fileInPtr, "%29s", str); //Assigns word to *char
str[29] = '\0'; //Optional: Whole line
pStrPtr = convertToPigLatin(str, pStr);
fprintf(fileOutPtr, "%15s\t\t\t\t%15p\n", str, *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] = {'\0'};
char cStr[MAX_STR_SIZE] = {'\0'};
char dStr[] = {'-','\0'};
char ayStr[] = {'a','y','\0'};
char wayStr[] = {'w','a','y','\0'};
pStrPtr = pStr;
while (*strPtr != '\0')
{
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(cStr,ayStr);
strcat(pStr, cStr);
}
//printf("%s\n", pStr);
return pStr;
}