我想要另一个条件 - 仍然保持快速执行时间但更安全 - 如果其中一个或两个字符串为空,我返回false:
int speicial_strcmp(char *str1, char* str2 )
{
if(*str1==*str2 =='\0')
return 0;
return strcmp(str1,str2);
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个PHP页面,将上传的文件转换为XML.我只想将新闻文件转换为XML.唯一需要转换的文件是news.htm.我把问题缩小到这个if语句.这有什么问题?
$fileName = basename( $_FILES['uploaded']['name'] );
if( strcmp( $fileName, "news.htm") == 0 )
(
//convertToXML();
)
Run Code Online (Sandbox Code Playgroud) 为什么if语句总是正确的?
char dot[] = ".";
char twoDots[] = "..";
cout << "d_name is " << ent->d_name << endl;
if(strcmp(ent->d_name, dot) || strcmp(ent->d_name, twoDots))
Run Code Online (Sandbox Code Playgroud)
我用strcmp错了吗?
我正在理解strcmp函数的奇怪行为,将通过以下代码进行说明:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char *p = "no";
cout << p << endl; //Output: no
cout << &p << endl; //Output: 0x28ac64
cout << strlen(p) << endl; //Output: 2
cout << strcmp(p, "no") << endl; //Output: 0
cin >> p; //Input: bo
cout << p << endl; //Output: bo
cout << &p << endl; //Output: 0x28ac64
cout << strlen(p) << endl; //Output: 2
cout << strcmp(p, "no") << endl; //Output: 0 …Run Code Online (Sandbox Code Playgroud) 下面的代码在我的头文件中:
int mystrcmp(const char *s1, const char *s2) // strcmp function
{
while(*s1 == *s2)
{
if(*s1 == '\0' || *s2 == '\0')
break;
s1++;
s2++;
}
if(*s1 == '\0' && *s2 == '\0')
return (0);
else
return (-1);
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我运行它时,我的main.cpp表示它未通过2次测试
以下是我的main.cpp的摘录:
void testmystrcmp(void)
{
int iResult;
iResult = mystrcmp("Ruth", "Ruth");
ASSURE(iResult == 0);
iResult = mystrcmp("Gehrig", "Ruth");
ASSURE(iResult < 0);
iResult = mystrcmp("Ruth", "Gehrig");
ASSURE(iResult > 0); // right here mystrcmp fails the test
iResult = mystrcmp("", "Ruth");
ASSURE(iResult …Run Code Online (Sandbox Code Playgroud) 我可以选择指针比较或strcmp.我知道字符串永远不会超过8个字符,我的目标是64位平台.他们的表现同样出色还是其中一个会更好?我可以想象这可能在平台和编译器之间有所不同,如果是这样,我想知道有关平台/编译器细节的详细信息.
GR,
科恩
好吧,这一定很容易,但我现在有一天:-p
我有一个char*数组,例如目的如下:
char* symbols [] = {"++-", "--+"};
Run Code Online (Sandbox Code Playgroud)
我在试图环symbols[0]和symbols[1]和比较每个字符,看看它的一个+或-
我尝试着:
char* tmp = symbols[0];
for (int i = 0; i < strlen(tmp); i++)
{
if(strcmp(tmp[i], "+") == 0)
{
printf("It's a plus!\n");
}
else if (strcmp(tmp[i], "-") == 0)
{
printf("Its a minus!\n");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,编译时我收到警告:
comparison between pointer and integer [enabled by default]
Run Code Online (Sandbox Code Playgroud)
我想我错过了一些基本的东西 - 有人能帮我看看树上的木头吗?
好吧,我不确定这是我还是其他什么,但我真的很困惑.
我试图在另一个字符串中找到第一个字符串出现(就像C++中的InString()),但是从C开始,用strcmp()来做.
我有两个char数组,string []和toFind [],我在两个for()循环中通过它们比较每个char和strcmp().
这是代码:
int inString(char string[], char toFind[]){
int i_toFind, i_string, check = 0, start = -1;
for(i_toFind = 0; i_toFind < getLength(toFind)-1; i_toFind++){
for(i_string = 0; i_string < getLength(string)-1; i_string++){
if(strcmp(&string[i_string], &toFind[i_toFind])==0){
printf("%i & %i == %i\n", string[i_string], toFind[i_toFind], strcmp(&string[i_string], &toFind[i_toFind]));
if(start == -1){
start = i_string;
}
check++;
i_toFind++;
if(check == getLength(toFind)-1){
return start;
}
}
else{
printf("%i & %i == %i\n", string[i_string], toFind[i_toFind], strcmp(&string[i_string], &toFind[i_toFind]));
check = 0;
start = -1;
}
}
} …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <math.h>
#include <string.h>
#define size 7
int computeN(char s1[])
{
int n=-1;
if(strcmp(s1, "black") == 0)
{
n = 0;
}
else if (strcmp(s1, "brown") == 0)
{
n = 10;
}
else if (strcmp(s1, "red") == 0)
{
n = 20;
}
else if (strcmp(s1, "orange") == 0)
{
n = 30;
}
else if (strcmp(s1, "yellow") == 0)
{
n = 40;
}
else if (strcmp(s1, "green") == 0)
{
n = 50;
}
else …Run Code Online (Sandbox Code Playgroud) 所以这是我的代码,问题是当我在两个字段中输入密码'malik'时,我得到strcmp()输出,当它显然应该为0时,我会附加一个图像以使自己清楚.另外,我尝试使用var_dump($ upassword)和var_dump($ ucpassword),我得到了两个字符串(5),所以没有空格.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Sign-UP</title>
</head>
<body>
<form action="signup.php" method="post">
<input type="text" name="uname" placeholder="enter the username">
<input type="email" name="uemail" placeholder="enter the email" id="">
<input type="password" name="upassword" placeholder="Password">
<input type="password" name="ucpassword" placeholder="confirm password">
<button type="submit" name="registor">Registor</button>
</form>
</body>
</html>
<?php
if (isset($_POST['registor'])) {
$uname = $_POST['uname'];
$uemail = $_POST['uemail'];
$upassword = (string)$_POST['upassword'];
$ucpassword = (string)$_POST['ucpassword'];
echo($uname."<br>");
echo($uemail."<br>");
echo($upassword."<br>");
echo($ucpassword."<br>");
if($uname == '' || $uemail == '' || $ucpassword …Run Code Online (Sandbox Code Playgroud)