- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些代码在一个函数中大量使用 strcasecmp
(参见版本 1)。所以我想知道我是否应该将它更改为大写或小写和 strcmp
(如版本 2)以兼容 c 并具有更好的性能,因为我只需要将其转换为大写- 或者小写一次。
char pname[5]="TEST";
//version 1
if(strcasecmp(pname,"TEST")==0)
{
printf("%s\n", pname);
}
//version 2
for(int i=0;i<5;++i)
{
pname[i]=toupper(pname[i]);
}
if(strcmp(pname,"TEST")==0)
{
printf("%s\n", pname);
}
最佳答案
我假设更好,你的意思是,更好的性能,即更少的操作。
假设:
x = time of tolower()/toupper()
n = time of string[n] operation
还有
best case (quickest time to complete) = no match
worst case (longest time to complete) = string match
这些计算假设我们只需要降低/提升一个字符串,如您的代码所示
版本 1:(strcasecmp)
best case = 1 + x [i.e. one operation on to lower/upper on first character - no match]
worst case = n + (x * n) [i.e. n operations of to lower/upper - string match]
-- average = (n*(x+1) + x + 1)/2
版本 2(tolower/upper + strcmp)
best case = 1 + (x * n) [i.e. n operations of to lower/upper on all character - no match]
worst case = n [i.e. n operations on all characters]
-- average = (1 + n*(x+1)) / 2
结论
所以与版本 2 的不同之处在于,它改进了最坏的情况,但这样做却使最好的情况变得更糟。然而,在一般情况下,版本 2 更快。
即如果您像之前的版本 2 一样将整个字符串更改为大写或小写,那么您必须已经遍历字符串中的每个字符,这可能不需要,因为字符串可能根本不匹配,添加额外的操作不需要。
此外,根据您使用的编译器,使用索引的 for 循环不是特别快,您可以更快:
while (*chptr)
{
// do stuff with *chptr
chptr++;
}
对于字符串。
关于c - strcasecmp 或 tolower/toupper + strcmp 哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51418990/
使用 String.toUpper() 时尝试“格式化”不包含大写字符(例如阿拉伯语)的语言时,是否必须采取任何其他预防措施? string arabic = "مرحبا بالعالم"; str
对于以下功能 void display() { for (int i = 0; i 97 && (i+j)%2 == 0) SetConsoleTextAtt
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
这里我检查输入数组 (inputText[]) 中的每个字符是否至少与 LETTERS 数组中的一个字母匹配。但 toupper() 函数对我不起作用。我正在 Linux mint 上工作并使用 gc
我在尝试理解 toupper() 函数时面临一个有趣的问题。请找到下面的代码:这里的问题是在输出中我总是缺少输入字符串的第一个字符。 请纠正我的错误。我在谷歌搜索了太多之后才发布此内容。 void U
这个问题已经有答案了: Lowercase characters to Uppercase characters in C & writing to file (1 个回答) 已关闭 7 年前。 我正
我尝试了以下方法:*string = toupper(*string); 这仅使指针指向的字符串指针的第一个字符变为大写。我希望 *pointer 指向的所有字符指针均为大写。无论如何我可以做到这一点
我正在从服务器传递一条消息,该消息存储在一个名为 strObject 的字符串变量中。我希望将 strObject 中的字符串转换为大写。所以,我使用 ToUpper() 方法。但是,当我添加一个断点
我正在使用 substring 和 IndexOf 来定位字符串中的值,但如果下面的字符串 (strOldValue) 包含不同大小写的任何字符串,我的代码将不起作用。因此,如果 strOldValu
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Convert a String In C++ To Upper Case 你好,我需要一个可移植的函数来将
我正在尝试将字符串转换为大写以便我可以对其进行操作,但是虽然我可以成功地操作自然大写字符串以及将小写转换为大写,但使用这种转换方法无法进行操作。 例如,如果我通过加密传递“hello”,我的加密字符串
我已经创建了一组算法,它接受一个字符串 vector 的输入,检查是否有任何字符串出现不止一次:如果是这样,从 vector 中删除所有额外出现的字符串,然后输出新的,'lighter ' 没有冗余的
我实现了 toUpper()。它并非 100% 有效。 代码: char* toUpper(char* string) { char* sv = string; while(*sv++
我很好奇 .ToUpper() 是如何工作的?是否存在某种映射,较低的 a 具有 UTF 代码 XYZ,而较高的 a 具有 UTF 代码 XYZ1? 最佳答案 是的,它正在使用 Unicode 元数据
我有一个 []byte,b,我想选择一个字节,b[pos] 并改变它大写(然后是小写) bytes 类型有一个名为 ToUpper() 的方法。如何将它用于单个字节? 在单个字节上调用 ToUpper
我在使用 toupper() 函数时遇到问题: 代码: #include #include using namespace std; int main (){ string input {
如果 ToUpper() 不存在,你会怎么写? i18n 和 L10n 的奖励积分 由此引发的好奇心:http://thedailywtf.com/Articles/The-Long-Way-toUp
我见过以下使用自定义 toupper() 函数的程序。 #include void my_toUpper(char* str, int index) { *(str + index) &=
长期潜伏者需要帮助,但我无法通过搜索找到。好吧,我在撒谎,我发现了一些东西,但它对我没有帮助。 我想做的是创建一个包含字符串的 char 数组并显示,将它们传递给一个函数,该函数将大写字母字符(应该只
我被分配重新编码 toUpper 和 toLower 函数。我是否必须在 if 语句中写出所有 26 个字母,例如: if ( char == 'a' ) return 'A'; 或者还有其他简单的方
我是一名优秀的程序员,十分优秀!