1

所以我认为我离这里更近了,但在打印反向字符串时我仍然会得到有趣的结果。我会尽量详细的。

这是输入:

Writing code in c
is fun

这是我想要的:

c in code Writing
fun is

这是实际的输出:

C
 in code Writing
fun
 is

这是我的代码:

char str[1000];  /*making array large. I picked 1000 beacuse it'll never be written over.  A line will never hole 1000 characters so fgets won't write into memory where it doesnt belong*/

int reverse(int pos)
{
    int strl = strlen(str)-1,i;
    int substrstart = 0,substrend = 0;
    char temp;

    for(;;)
    {
            if( pos <= strl/2){    /*This will allow the for loop to iterate to the middle of the string. Once the middle is reached you no longer need to swap*/ 
                    temp = str[pos];        /*Classic swap algorithm where you move the value of the first into a temp variable*/
                    str[pos]= str[strl-pos]; /*Move the value of last index into the first*/
                    str[strl-pos] = temp;   /*move the value of the first into the last*/
            }

            else
            break;
    pos++;  /*Increment your position so that you are now swaping the next two indicies inside the last two*/
    }       /* If you just swapped index 5 with 0 now you're swapping index 4 with 1*/


    for(;substrend-1 <= strl;)
    {
    if(str[substrend] == ' ' || str[substrend] == '\0' ) /*in this second part of reverse we take the now completely reversed*/ 
            {
            for(i = 0; i <= ((substrend-1) - substrstart)/2; i++)       /*Once we find a word delimiter we go into the word and apply the same swap algorthim*/
                    {
                    temp = str[substrstart+i];              /*This time we are only swapping the characters in the word so it looks as if the string was reversed in place*/
                    str[substrstart+i] = str[(substrend-1)-i]; 
                    str[(substrend-1)-i] = temp;
                    }
    if(str[substrend] == '\t' || str[substrend] == '\n')
    {
    str[substrend] = ' ';   
     for(i = 0; i <= ((substrend-1) - substrstart)/2; i++)      /*Once we find a word delimiter we go into the word and apply the same swap algorthim*/
                    {
                    temp = str[substrstart+i];              /*This time we are only swapping the characters in the word so it looks as if the string was reversed in place*/
                    str[substrstart+i] = str[(substrend-1)-i]; 
                    str[(substrend-1)-i] = temp;
                    }
    }   
            if(str[substrend] == '\0')
            {
                    break;
            }
            substrstart=substrend+1;
            }
    substrend++;    /*Keep increasing the substrend until we hit a word delimiter*/
    }
printf("%s\n", str);   /*Print the reversed line and then jump down a line*/
   return 0;
}


int main(int argc, char *argv[]) 
{

char *filename;  /*creating a pointer to a filename*/
FILE *file20;   /*creating FIlE pointer to a file to open*/
int n;
int i;

if (argc==1) /*If there is no line parameter*/
{
printf("Please use line parameter!\n");
return(5); /*a return of 5 should mean that now line parameter was given*/
}

if(argc>1){
for(i=1; i < argc; i++)
{
filename = argv[i]; //get first line parameter
file20 = fopen(filename, "r"); //read text file, use rb for binary

    if (file20 == NULL){
    printf("Cannot open empty file!\n");
    }

    while(fgets(str, 1000, file20) != NULL) {
    reverse(0);
    }

fclose(file20);

}
return(0); /*return a value of 0 if all the line parameters were opened reveresed and closed successfully*/ 

}
}

谁能指出我的反向函数逻辑中的错误?

4

2 回答 2

0

您所写的内容将整个文件读出到一个缓冲区中,并立即在整个文件上运行您的反向函数。

如果您希望将第一行反转,然后将下一行反转,等等,您需要使用 fgets 之类的东西一次读取一行。在每一行上反向运行,一次一个,你应该得到你想要的。

http://www.cplusplus.com/reference/cstdio/fgets/

于 2012-12-05T10:06:30.543 回答
0

假设您想继续将整个文件读入单个缓冲区,然后一次对缓冲区进行逐行反转(而不是读取一行,反转它,读取下一行,反转它,等等),你需要重新编写你的 reverse() 算法。

你所拥有的似乎已经奏效了;我认为您可以通过围绕现有逻辑添加另一个循环来获得所需的内容,并对现有逻辑进行一些修改。从指向 str[] 开头的指针开始,我们称之为char* cp1 = str. 在这个新循环的顶部,创建另一个指针 ,char* cp2并将其设置为等于 cp1。使用 cp2,扫描到当前行的末尾以寻找换行符或 '\0'。现在你有一个指向当前行开头的指针(cp1)和一个指向当前行结尾的指针(cp2)。现在修改您现有的逻辑以直接使用这些指针而不是 str[]。您可以通过简单地计算当前行的长度lineLen = cp2 - cp1;(您不想使用 strlen() 因为该行可能没有终止的 '\0')。之后,它将循环回到新循环的顶部并继续下一行(如果 *cp2 不指向 '\0')...只需设置 cp1 = cp2+1 并继续下一行线。

于 2012-12-05T10:19:50.303 回答