标签: strcmp

C - 条件语句&strcmp&NULL字符串

做以下事情是多么安全:

if (flag_val != NULL && strcmp (val, flag_val) == 0) {
   // something
} else {
   // something else
}
Run Code Online (Sandbox Code Playgroud)

知道有时flag_valNULL,有时不会.

我知道它会首先检查flag_val != NULL,如果它评估为假,它不应该检查第二个条件对吗?

谢谢

c null strcmp

0
推荐指数
1
解决办法
1744
查看次数

strcmp混乱

据我所知,标准strcmp函数看起来像这样:

int strcmp(const char* string1, const char* string2)
{
    while(*pString1++ == *pString2++)
    {
        if(*pString1 == 0) return 0;
    }
    return *pString1 - pString2;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,这不会增加传递给strcmp的指针吗?在下面的示例中,它似乎会丢弃指针并导致无效的事情发生.

const char* string1 = "blah";
const char* string2 = "blah";
const char* string3 = "blah";
if(strcmp(string1, string2) {doSomething();} 
// Won't this make string1 = "" because it incremented the pointer to the end?
else if (strcmp(string1, string3) {doSomethingElse();}
Run Code Online (Sandbox Code Playgroud)

对不起,我只是感到困惑,因为看起来如果我将指针传递给strcmp,我不应该指望突然持有空字符串的指针.似乎strcmp应该采用const char*const.我完全误解了什么吗?

c++ string char strcmp

0
推荐指数
1
解决办法
886
查看次数

无法将嵌套if转换为if(condition1 || condition2)

我有以下代码正常运行.

if((strcmppgm2ram((char*)name, (ROM char*)"products.htm") != 0))
{
    if((strcmppgm2ram((char*)name, (ROM char*)"restock.htm") != 0))
    {
        return HTTP_IO_DONE;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想清理它并把它放在表格中:

if((strcmppgm2ram((char*)name, (ROM char*)"products.htm") != 0) || (strcmppgm2ram((char*)name, (ROM char*)"restock.htm") !=0))
{   
        return HTTP_IO_DONE;    
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,后者无法正常工作.有什么我忽略了?提前致谢!为了这个问题,PS strcmp == strcmppgm2ram.

c if-statement strcmp

0
推荐指数
1
解决办法
105
查看次数

Strcmp in C不工作

我是C的新手并且在下面的程序中遇到了麻烦.它询问人们他们是否健康并相应地显示消息.到目前为止,我已尝试使用!strcmp,strcmp并且strncmp没有返回正值if,所有都跳到else语句.任何人都可以指出我出错的地方,因为语法对我来说似乎很好.

#include "stdafx.h"
#include <stdio.h>
#include <string.h>

int main()
{
   char well[3];

   printf("Hello, are you well today? (Yes/No) ");
   scanf_s ("%s",well);
       if (!strcmp(well,"Yes")){
           printf("Glad to hear it, so am I\n");
   }
   else{
       printf("Sorry to hear that, I hope your day gets better\n");
   }   
system("PAUSE");   
return 0;
}
Run Code Online (Sandbox Code Playgroud)

非常感谢所有人的答案,不幸的是他们似乎都没有工作.分配4以考虑空值没有区别.调用scanf而不是scanf_s导致访问冲突(这是奇怪的,因为程序的其余部分使用普通scanf.在scanf_s中添加'4'参数也没有区别.真的在这里撕扯我的头发我很乐意接受在行的末尾为null但程序似乎无法识别它.

c strcmp

0
推荐指数
1
解决办法
2580
查看次数

在if语句中使用strcmp

我对C很新,我试图理解使用字符串并strcmpif语句中比较两个字符串.

我的目标是能够根据用户输入的内容运行不同的功能.

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>

void gasbill();
void electricitybill();

int main()
{
  char input[20];
  const char gasCheck[4] = "gas";
  const char electricityCheck[13] = "electricity";

  printf("Your bills explained!\n\n");
  printf("In this application I will go through your gas and electricty bills.\n");
  printf("I will explain how each of the billing payments work, \nand the calculations that go on,\n");
  printf("to create your bill.\n\n");
  printf("Please choose a bill to get started with:\n- gas\n- electricity\n\n");
  fgets(input, 20, …
Run Code Online (Sandbox Code Playgroud)

c string strcmp

0
推荐指数
1
解决办法
1万
查看次数

在c中转换为小写后比较字符串

我需要比较两个字符串的相等性(不区分大小写)但我的实现在编译时返回了很多警告.

我的实施:

//The word array will contain any number of strings of varying lengths
//string is the word to compare to
char **wordArray, char*string;

int i, sizeOfArray = 10

for(i = 0; i < 10; i++)
{
    //Return 1 if the string is seen in the array 
    if(strcmp(tolower(wordArray[i]), tolower(string)) == 0)
        return 1;
}

return 0;
Run Code Online (Sandbox Code Playgroud)

我收到这些警告:

warning: passing argument 1 of ‘tolower’ makes integer from pointer without a cast [enabled by default]

note: expected ‘int’ but argument is of …
Run Code Online (Sandbox Code Playgroud)

c strcmp tolower

0
推荐指数
1
解决办法
3153
查看次数

如何在C程序中检查字符串是否有/字符

我正在写一个简单的C程序

char *path;
Run Code Online (Sandbox Code Playgroud)

我想检查路径末尾是否有尾随"/",我尝试了strcmp功能,但它给了我分段错误.

if (strcmp(path[strlen(path)-1], "/") == 0)
Run Code Online (Sandbox Code Playgroud)

如何检查字符串是否"/"在末尾

谢谢.

c string comparison path strcmp

0
推荐指数
1
解决办法
2993
查看次数

拼写检查程序问题

我正在尝试完成拼写检查项目并遇到一些问题.我正在尝试使用strlenstrcmp比较文章和字典中的单词,但编译器告诉我标识符"strlen"和"strcmp"是未定义的.我不知道该怎么做.此外,当我加载char(article)int ArticleLength,它告诉我,我不能烧焦文章INT使用.我是一个初学者,需要一些帮助.到目前为止,这是我的代码.

#include <stdio.h> // provides declarations for printf and putchar
#include <stdint.h> // provides declarations for int32_t uint32_t and the other (new) standard C type
/* You must write this function (spellCheck). Do not change the way the function is declared (i.e., it has
 * exactly two parameters, each parameter is a standard (mundane) C string (see SpellCheck.pdf).
 * You are expected to use reasonable programming style. I *insist* that you …
Run Code Online (Sandbox Code Playgroud)

c dictionary spell-checking strlen strcmp

0
推荐指数
1
解决办法
1532
查看次数

C程序挂在strcmp附近

我正在构建一个client-server socket模拟,CTCP connection从客户端接受一个,然后客户端向我的服务器发送一条消息.我正在成功接收消息,然后我遍历我arraystructs寻找字符串匹配

struct Mapping
{
  char  key[6];
  char  value[8];
} MapElement;

int main
{
  char buf[BUFSIZE]; /* BUFSIZE 2048 */
  int size = 4;
  struct Mapping serverMap[size];
  initialize_server_map(serverMap); /* This method works, so not displaying in SO */

  /* ... socket stuff (this works as seen from print statements) */

  recvlen = recv(rqst, buf, BUFSIZE, 0);
  printf("The received length is %d\n", recvlen);
  printf("The buf is: %s\n", buf);
  if (recvlen …
Run Code Online (Sandbox Code Playgroud)

c sockets strcmp

0
推荐指数
1
解决办法
183
查看次数

如果C中的语句总是返回false

我是C的新手,所以我不太熟悉它的语法,但是我调试了我的代码并研究了正确的语法,它似乎是正确的,我也改变了变量的范围,看看这是不是导致错误.

if语句应该比较两个变量,它们都保存字符串,我甚至打印出两个变量以确保它们是相同的,但是它仍然直接跳到if语句的else部分.任何人都可以给我任何关于为什么它不会运行if语句的指针,它只是直接跳到'不正确'.

correctWord变量在代码中的一个不同的部分中定义.

在此查找完整代码.

-UPDATE-

我现在已经更新了代码的语法,但它仍然返回false.

输出屏幕

char correctWord[20];

void userGuess(){
    char userWordGuess[20];

    printf("Anagram: ");
    printf(anagramWord);
    printf("Your Guess: ");
    scanf("%s",userWordGuess); //Reads in user input

    printf(correctWord);
    printf(userWordGuess);

    if(strcmp(userWordGuess, correctWord) == 0){
        printf("Congratulations, you guessed correctly!");
    }else{
        printf("Incorrect, try again or skip this question");
    }
}
Run Code Online (Sandbox Code Playgroud)

c if-statement c-strings strcmp

0
推荐指数
1
解决办法
351
查看次数