- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我认为我离这里更近了,但在原地打印反转字符串时我仍然得到有趣的结果。我会尽量详细。
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*/
}
}
任何人都可以指出我的反向函数逻辑中的错误吗?
最佳答案
您所写的内容将整个文件读出到一个缓冲区中,并立即对整个文件运行反向函数。
如果您想要反转第一行然后反转下一行,等等,您需要使用类似 fgets 的方法一次读取一行。反向运行每一行,一次一个,你应该得到你想要的。
关于c - 需要就地反转文件,但它只适用于一行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13718624/
我正在尝试解决以下问题: We are given an array containing ‘n’ objects. Each object, when created, was assigned a
考虑以下代码: a=(1 2 3) a='seven' export a declare -p a 输出(来自declare)是: declare -ax a='([0]="seven" [1]="2
我正在尝试将 ['1','2','3','4'] 转换为 [1,2,3,4]我想就地进行此转换。有可能做到吗?如果不是,最佳解决方案是什么。 最佳答案 我觉得用map比较好对于这类任务。这会创建迭代器
好的,所以我之前发布了关于尝试(没有任何预建函数)删除额外空间的信息 "this is a test"会回来的 "this is a test" Remove spaces from a strin
我有一个名为Media的插件,该插件应负责图像大小调整等工作。 它具有以下依赖性: dependencies { compile group: 'org.ccil.cowan.tagsoup'
我需要将一个大字符串向左“移动”X 个空格。它太大了,无法放入内存,所以我需要就地做。我需要使用最少量的系统调用来完成此操作。 我知道我可以使用缓冲区并重用内存来最大限度地减少内存消耗,然后使用 fs
我想知道是否可以在不需要临时数组的情况下通过 Cholesky 分解获得矩阵的逆。截至目前,我可以在不使用临时数组的情况下进行 cholesky 分解,但从那里我还没有想出一种方法来获得原始矩阵的逆矩
是否有任何用于 Javascript 的就地编辑插件..像 firebug 之类的东西,它对即时 CSS 编辑和预览非常有用,但不允许就地 JS 编辑..那么,有没有我们可以立即更新和更新的工具或插件
题目如下:给定一个 linked list,将备用 indices 移到 list 的后面 例如: input: : [0] -> [1] -> [2] -> [3] -> [4]
在我看来,std::copy_if 对于过滤容器非常有用: std::vector vec { 1, 2, 3, 4 }; auto itEnd = std::copy_if(vec.begin(),
在 C++ 中相交两个集合的标准方法是执行以下操作: std::set set_1; // With some elements std::set set_2; // With some othe
在 Python 中,字符串是不可变的。 逐个字符遍历字符串并对其进行修改的标准习语是什么? 我能想到的唯一方法是一些与加入结果字符串相关的真正臭名昭著的黑客攻击。 -- 在 C 中: for(i
我有一个 ListBuffer。我想删除满足特定条件的所有元素。 我可以迭代它并删除每个元素。但是 Scala 对改变你正在迭代的列表有什么看法呢?它会起作用,还是会删除错误的元素/不返回所有元素?
我需要重新绑定(bind)两个大数据帧。现在我用的是 df 根据 nikola 的评论,这里是 ?rbindlist 的描述(v1.8.2 中的新增功能): Same as do.call("rbi
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我在带有 LVS_EDITLABELS 的无模式 Win32 对话框中有一个小图标模式的 ListView 放。无论编辑是通过单击鼠标还是通过调用 ListView_LabelEdit() 以编程方式
所以基本上不能/允许创建一个新数组。除了实际更改和操作当前数组外,无法返回任何内容。您如何获取字符数组并简单地翻转/反转它们。 Starting array: ['P','e','r','f','e'
我不明白为什么下面的代码没有对 vector 的前两个元素进行排序: int main() { std::vector v = {2,1,3,1,2}; std::sort(v.beg
我有以下(简化的)代码: a = a[::3] b = b[::3] c = c[::3] d = d[::3] a,b,c,d,其实都是很复杂的表达式,所以我想这样写: for l in [a, b
可以对数组进行不依赖于数组秩的操作。迭代器也不总是合适的解决方案。给定数组 double[,] myarray = new double[10,5]; 实现以下工作流程是可取的: 将 Rank>1 的
我是一名优秀的程序员,十分优秀!