比较用户的字符串
Compare string from the user
我正在尝试从链接列表中删除一个节点。节点具有用户输入的名称。但是我无法弄清楚如何在循环时忽略大写/小写。这是我的代码。
void del(string e)
{
temp=new node;
temp=head;
temp2=new node;
temp2=temp->next;
if(temp->info==e)
{
head=temp->next;
delete temp;
}
else
{
while(temp2->info!=e)
{
temp=temp->next;
temp2=temp2->next;
}
temp2=temp2->next;
temp->next=temp2;
}
}
我得到了这个
的字符串cout<<"Enter the name to delete"<<endl;
ws(cin);
getline(cin,e);
del(e);
因此,有什么办法可以忽略大写/小写字母,而循环和if语句?
两个字符串的敏感比较的窍门是将两个字符串转换为较低或上情况,然后比较。
。不幸的是,STL不能为案例转换提供非常方便的方法。因此,这里有一些可能性:https://stackoverflow.com/a/313990/1143850。只是从那里复制:
#include <algorithm>
#include <string>
std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
所以,在您的情况下,
string lowerE = std::transform(e.begin(), e.end(), e.begin(), ::tolower);
...
while(std::transform(temp2->info.begin(), temp2->info.end(), temp2->info.begin(), ::tolower) != lowerE) ...
当然,您可以创建一个函数来简化它或在那里使用其他转换方法。
,另一种可能性肯定是创建您自己的比较功能,并使用 tolower
或 towlower
函数来比较char。
您无需手动转换被转换的字符串的情况。如果您要处理字符串,请使用strcmp。对于情况不敏感的检查,您可以使用_strcmpi。
,例如
if(!strcmp(String1, String2)) { .... }
如果strcmp返回0(fals),则有一个匹配,并应用了案例灵敏度。
对于没有情况敏感性的比较,您使用_strcmpi。
,例如
#include <Windows.h>
#include <iostream>
using namespace std;
BOOL StringMatch(
CONST CHAR *CmpString,
CONST CHAR *CmpString2
)
{
return (!_strcmpi(CmpString,
CmpString2)) ? TRUE : FALSE;
}
int main()
{
if (StringMatch("hello", "HELLO"))
{
cout << "Match without case sensitivityn";
}
getchar();
return 0;
}
由于您使用的是std :: string,因此可以使用.c_str()。
,例如
string hellostring = "hello";
if (StringMatch(hellostring.c_str(), "HELLO"))
{
cout << "Match without case sensitivityn";
}
如果您需要切换到Unicode编码而不是ASCII,则有WCSCMP和_WCS*/WCS*。
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 使用用户定义函数的字符串反转
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 用户输入字符串的文件附加问题..C++
- 从用户定义的类生成格式字符串?
- 我如何使它,无论用户用空白字符串按 Enter 多少次,它总是打印"开始"字符串?
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 如何计算字符串中用户输入元素的出现次数?
- 是否可以创建一个用户定义的文本,将字符串文本转换为 own 类型的数组?
- 如何使用用户输入创建字符串队列?
- 正在尝试将用户输入传递到字符串(C++)中
- 将用户输入从 editLine 记录到字符串数组
- 是否可以用类似C/C++(或任何语言)的语言,从作为用户输入的字符串或文件中创建用户定义的数据类型
- 如何删除用户输入字符串末尾的空字符?
- C++代码,用于在用户插入的字符串的每个元音之后插入一对字符
- 用户输入字符串的结构的搜索向量
- 有没有办法通过作为用户输入的字符串来创建类实例名称?
- 如何在不使用cin.ignore()和clear()的情况下修复字符串用户输入--C++
- C++字符串用户输入类对象
- 有什么方法可以在编译时检查字符串用户定义的文字吗