所以我认为我离这里更近了,但是当打印反向弦时,我仍然会得到有趣的结果。我会尽力详细说明。
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*/
}
}
有人能指出我的反向功能逻辑错误吗?
答案 0 :(得分:0)
您所写的内容将整个文件读入一个缓冲区,并立即对整个文件运行反向函数。
如果您希望第一行反转,然后下一行反转等,您需要使用类似fgets的内容一次读取一行。每行反向运行一次,你应该得到你想要的东西。
答案 1 :(得分:0)
假设您想继续将整个文件读入单个缓冲区,然后一次性对缓冲区进行逐行反向(而不是读取一行,反转它,读取下一行,反转它,依此类推),你需要重新编写你的reverse()算法。
你现有的东西似乎已经有效了;我认为您可以通过在现有逻辑上添加另一个循环来获得所需的内容,并对现有逻辑进行一些修改。从指向str []开头的指针开始,我们称之为char* cp1 = str
。在这个新循环的顶部,创建另一个指针char* cp2
,并将其设置为等于cp1。使用cp2,扫描到当前行的末尾寻找换行符或'\ 0'。现在你有一个指向当前行开头的指针(cp1)和一个指向当前行结束的指针(cp2)。现在修改现有逻辑以直接使用这些指针而不是str []。您可以通过lineLen = cp2 - cp1;
计算当前行的长度(您不希望使用strlen(),因为该行可能没有终止'\ 0')。之后,它将循环回到新循环的顶部并继续下一行(如果* cp2未指向'\ 0')...只需设置cp1 = cp2 + 1并继续下一行线。