试图在字符串中找到一个数字,更改它,放回并找到下一个数字
Trying to find a number in a string, change it, put it back and find the next number
如果这个问题已经得到回答,我很抱歉。如果是,请告诉我解决方案。
我目前正在尝试遍历一个文本文件,对其进行迭代以找到一个数值,并将该值管道传输到子/父进程中,然后在那里将更新后的值返回到文本中。尽管很简单,我还是试图找到数字,然后将它们放入一个数组/字符串中,这样我就可以将其输入到管道中,然后将其反馈到文本中。我已经做了5天了,所以我终于向你们寻求帮助了。再次,如果这种情况已经存在,请道歉。
我目前正在浏览文本,并检查每个字符是否为整数。我可以把它放进一根绳子里,但它只是一根长长的大绳子。我不知道如何实现它。
这就是我到目前为止所拥有的。
while(!readText.eof())
{
while(getline(readText, item))
{
readText >> item;
for(int i=0; i < item.length(); ++i){
if(isdigit(item[i])) //checking if it is a digit
newitem += item[i]; //puts into string
if(!isdigit(item[i])) // if it's not a digit feed it out (but maybe I
//should put into new variable?
cout << item[i];
else if(isspace(item[i]))//keeps the spaces as cout, but again, maybe
//put it into a variable?
cout << " ";
}
}
}
我面临的挑战是试图弄清楚如何将这些价值观分开并放回原处。我会随着指数的增长而增长,但数字会增长,所以位置不会那么准确。
更新的上下文
感谢大家的帮助。一个示例文件是
In the b6765lue sky there are 59 clouds. The gra7ss is green 766 and it 837 has 7 a cow in i87t.
我需要通过一个过程/函数来更改数字。就像你提到的,数字的大小自然会变大。测试文件可以是100行。
In the b6789lue sky there are 83 clouds. The gra31ss is green 790 and it 861 has 31 a cow in i111t.
这些值已随5个过程和增量值5进行了更改。
如果到达起始位置,为什么不保存一个值和一个新值的起始位置以进行打印?所以,在我看来:
struct replace_integer
{
int old_int_length;
int new_int_value;
int int_to_replace_text_position;
};
然后,当你打印下来的时候,跳过旧的int,打印新的。它行得通吗?
要记住起始位置,您应该修改您的周期:
int item_length = item.length();
for(int i=0; i < item_length ; ++i)
{
if(isdigit(item[i]))
{
int_to_replace_text_position = i;
while(isdigit(item[i++]) && i < item_length )
newitem += item[old_int_length_counter]; //puts into string
old_int_length = i - int_to_replace_text_position; //check it, i may have a 1 position mistake, too sleepy, so I can lose "-1"
}
if(!isdigit(item[i])) // if it's not a digit feed it out (but maybe I
//should put into new variable?
cout << item[i];
else if(isspace(item[i]))//keeps the spaces as cout, but again, maybe
//put it into a variable?
cout << " ";
}
相关文章:
- 重新定位图像时如何前进到下一个内存块
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- CIN 仅在输入非数字值时跳过下一个 CIN
- 查找最小的下一个更大的元素
- 转到基于范围的 for 循环中的下一个迭代器
- 如何检查流中文件的下一个单词是否为 alpha?
- 如何打印下一个字母直到 Z 并继续到 A?
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- Fibbonaci 递归代码返回错误值,始终返回下一个数字
- 我该如何将一个数字的下一个50转
- rand() 函数通过升序到前一个数字来生成下一个数字
- 试图在字符串中找到一个数字,更改它,放回并找到下一个数字
- 如何将数字向上舍入到下一个整数
- 将下一个数字相乘
- 按字典顺序从范围中获取下一个数字(不包含所有字符串的容器)
- 我不能删除双向链表中的最小数字,然后删除下一个
- 如何找到最近的下一个/上一个双精度值(给定数字的numeric_limits::epsilon)
- 具有一个零位的下一个更高的数字
- 给定一个数字,打印具有相同个数的下一个最高的数字