如何在 Java 或 C++ 中检查输入 .txt 文件的大写或小写字母?
How do I check an input .txt file for upper or lower case letters in Java or C++?
我发现了很多关于将字母从小写转换为大写或反之的问题,但我有一个稍微不同的问题:
我想编写一个程序,将 .txt 文件作为输入,读取它们并检测文本中字符从大写转换为小写的所有位置。例如读取输入文件:
Test input.txt:
AJHFOJEOJkljklfjddejhjhoIOJOJFOJlkmlkloisjdo...
程序会告诉我文本在位置 10 和 30 处从大写字母切换为小写字母。
我知道,这在常规英文文本中没有意义,因为文本不会经常从大写更改为小写。但是,我的程序不应该读取用英语编写的文本,而是读取包含 DNA 序列的文本,其中基因的所有外显子均以大写字母书写,所有内含子均以小写字母书写。简而言之,我的程序应该将 DNA 序列作为输入(作为 .txt 文件),然后告诉我外显子与内含子接壤的所有位置。
下一步,程序应该输出并进一步处理它在第一步中检测到的所有位置的最后三个大写字母和前六个小写字母。因此,对于上面显示的测试 input.txt,它将输出:
EOJkljklf and FOJlkmlkl
该程序的目的是在我作为 .txt 文件提供的任何基因中找到所有所谓的 'splice donor sites',然后进一步分析它们。
(技术上的庞然大物:剪接供体位点是 mRNA 转录物 exon/intron 边界处的九个碱基序列 [外显子的最后三个碱基和以下内含子的前六个碱基] 其中U1 snRNA 结合并启动剪接过程。
我假设您在 Linux 环境中,并且您正在命令行终端上使用该程序。
这是一个简单的解决方案:
#include <fstream>
#include <iostream>
int main (int argc, char **argv) {
int i, line_size;
std::ifstream myfile;
std::string token;
// Check if user put an input file as argument
if(argc < 2){
std::cout << "Usage : ./parser filename" << std::endl;
return 0;
}
// Open file and check for errors
myfile.open(argv[1]);
if(!myfile){
std::cout << "Error opening file" << std::endl;
return -1;
}
// For each line, put the line in variable token
while(getline(myfile, token)){
line_size = token.size();
for(i=2; i < line_size - 4; ++i){
// Go through the line and check for each character if it
// is an uppercase character and if the following one is
// a lowercase character. If so, print it.
if(std::isupper(token[i]) && std::islower(token[i+1])){
std::cout << token.substr(i-2, 6) << std::endl;
}
}
}
myfile.close();
return 0;
}
我发现了很多关于将字母从小写转换为大写或反之的问题,但我有一个稍微不同的问题:
我想编写一个程序,将 .txt 文件作为输入,读取它们并检测文本中字符从大写转换为小写的所有位置。例如读取输入文件:
Test input.txt: AJHFOJEOJkljklfjddejhjhoIOJOJFOJlkmlkloisjdo...
程序会告诉我文本在位置 10 和 30 处从大写字母切换为小写字母。
我知道,这在常规英文文本中没有意义,因为文本不会经常从大写更改为小写。但是,我的程序不应该读取用英语编写的文本,而是读取包含 DNA 序列的文本,其中基因的所有外显子均以大写字母书写,所有内含子均以小写字母书写。简而言之,我的程序应该将 DNA 序列作为输入(作为 .txt 文件),然后告诉我外显子与内含子接壤的所有位置。
下一步,程序应该输出并进一步处理它在第一步中检测到的所有位置的最后三个大写字母和前六个小写字母。因此,对于上面显示的测试 input.txt,它将输出:
EOJkljklf and FOJlkmlkl
该程序的目的是在我作为 .txt 文件提供的任何基因中找到所有所谓的 'splice donor sites',然后进一步分析它们。
(技术上的庞然大物:剪接供体位点是 mRNA 转录物 exon/intron 边界处的九个碱基序列 [外显子的最后三个碱基和以下内含子的前六个碱基] 其中U1 snRNA 结合并启动剪接过程。
我假设您在 Linux 环境中,并且您正在命令行终端上使用该程序。
这是一个简单的解决方案:
#include <fstream>
#include <iostream>
int main (int argc, char **argv) {
int i, line_size;
std::ifstream myfile;
std::string token;
// Check if user put an input file as argument
if(argc < 2){
std::cout << "Usage : ./parser filename" << std::endl;
return 0;
}
// Open file and check for errors
myfile.open(argv[1]);
if(!myfile){
std::cout << "Error opening file" << std::endl;
return -1;
}
// For each line, put the line in variable token
while(getline(myfile, token)){
line_size = token.size();
for(i=2; i < line_size - 4; ++i){
// Go through the line and check for each character if it
// is an uppercase character and if the following one is
// a lowercase character. If so, print it.
if(std::isupper(token[i]) && std::islower(token[i+1])){
std::cout << token.substr(i-2, 6) << std::endl;
}
}
}
myfile.close();
return 0;
}