我有这个功能
int does_exist_in_array(char team[], struct team *teams) {
int i;
for(i = 0; i < MAX_TEAMS_AMOUNT; i++) {
if(!strcmp(team, teams[i].name)) {
return 1;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时它崩溃了。有人知道出了什么问题吗?难道是我用错了?
这段代码在哪里做错了?我只需要char类型,请不要建议使用std::string.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *mystring="C:/windows";
char last_char;
last_char = mystring[strlen(mystring)-1];
cout<<"Input: " <<mystring<<endl;
if(strcmp(last_char,";")!=0)
{
strcat(mystring,";");
}
cout<<"Output: "<<mystring<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Compilation error time: 0 memory: 3340 signal:0
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *mystring="C:/windows";
^
prog.cpp:11:25: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
if(strcmp(last_char,";")!=0)
^
In file included from prog.cpp:2:0:
/usr/include/string.h:140:12: error: initializing …Run Code Online (Sandbox Code Playgroud) 我有一个项目,在我的代码中的某处我正在写这个:
else if (character[0] == '\'){
Run Code Online (Sandbox Code Playgroud)
如何将我的角色与此符号进行比较?我试图比较的所有其他符号,如,,;等等,这是我收到错误信息的唯一符号.
嗨,我在课堂上学到了string.h库,特别是strcmp函数,它比较了字符串.如果第一个字符串首先出现在字典中,它将返回一个大于0的数字,如果第二个字符串大于第一个字符串将返回小于0的数字,如果它们是等于它应该返回0. ive像这样使用它:
strcmp(strArr , strrev(strArr));
Run Code Online (Sandbox Code Playgroud)
随意教育我.
代码 :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100
#define PALINDROM_TRUE 0//because in strcmp if both strings compared are equile the strcmp will return 0.
int main(void)
{
char strArr[MAX_LENGTH];
printf("Enter string (max length 100 chars): ");
fgets(strArr , MAX_LENGTH , stdin);
int pali = strcmp(strArr , strrev(strArr));
if(pali == PALINDROM_TRUE)
{
printf("Palindrom\n");
}
else
{
printf("Not Palindrom\n");
}
system("PAUSE");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我进入即下面的代码"ABC"它输出到屏幕上回文它应该打印不回文,它永远不会打印不回文
我是C++编程的新手,在我的OPP课程中,我们被要求创建一个电话簿.
现在,教授在讲座中说了一些关于如果你想确保注入方法的变量没有被改变的话,你必须把const放在它上面.
到目前为止,这是我的代码.
private:
static int phoneCount;
char* name;
char* family;
int phone;
Phone* nextPhone;
public:
int compare(const Phone&other) const;
const char* getFamily();
const char* getName();
Run Code Online (Sandbox Code Playgroud)
在Phone.cpp中
int Phone::compare(const Phone & other) const
{
int result = 0;
result = strcmp(this->family, other.getFamily());
if (result == 0) {
result = strcmp(this->name, other.getName);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在比较函数中调用strcmp时,我不断得到"对象具有与成员不兼容的类型限定符".我知道我可以删除函数声明中的const,它会消失,但我仍然不明白为什么它首先显示出来.
非常感谢帮助.
为什么这段代码打印的是202?‘e’的ascii值为101,空字符的ascii值为0。那么它不应该打印101吗?
但是当我交换 s1 和 s2 时,我确实得到了 -101 作为答案。
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
char s1[50] = "ape";
char s2[50] = "ap";
printf("%d ", strcmp(s1, s2));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个程序,从字符串中删除元音,将它们添加到向量中,然后让用户再次拥有原始代码.
在我的代码中我有这个:
char s[20];
Run Code Online (Sandbox Code Playgroud)
我有这个比较后不久:
for(j=0;j<tam;j++)
{
if(strcmpi(s[j],"a")==1 ||
(strcmpi(s[j],"e")==1 ||
(strcmpi(s[j],"i") ==1||
(strcmpi(s[j],"o") ==1||
(strcmpi(s[j],"u")==1))
{
Run Code Online (Sandbox Code Playgroud)
因此,如果数组是char并且元音是char(""),为什么编译器会给我这个错误?:
[警告]传递`strcmpi'的arg 1使得整数指针没有强制转换
编辑
正如有人说正确的是s [j] =='a',但结果却是错误的.如果放车,结果仍然是车.不知道为什么.
if(s[j] == 'a' ||
s[j] == 'A' ||
s[j] == 'e' ||
s[j] == 'E' ||
s[j] == 'i' ||
s[j] == 'I' ||
s[j] == 'o' ||
s[j] == 'O' ||
s[j] == 'u' ||
s[j] == 'U')
{
s[j] = s[j++]; }
Run Code Online (Sandbox Code Playgroud) 我有一个程序读取文件,逐行检查,并逐字分解.我的问题是,我将把每个单词存储在一个数组中,但是我需要使用strcmp函数来验证这个单词是否已经存在.无论如何,下面是我的代码,我的问题是,为什么我的程序打印出1这么多次?我期待它只打印两次因为this在我的文本文件中出现两次.
while (fgets(line, sizeof(line), fi) != NULL) { // looping through each line
line_count += 1;
for (j = 0; j < sizeof(line); j++) { // convert all words to lowercase
line[j] = tolower(line[j]);
}
result = strtok(line, delimiters);
while (result != NULL) {
word_count += 1;
if (strcmp(result, "this")) {
printf("1\n");
}
result = strtok(NULL, delimiters); // get the next token
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的文字文件:
This is the first test.
This is the second …Run Code Online (Sandbox Code Playgroud) if(s.name=="kolkata")
{
printf("the details");
}
if(strcmp((s.name,"kolkata")==0)
{
printf("the details");
}
Run Code Online (Sandbox Code Playgroud)
第一个"if"情况没有语法错误仍然无法正常工作,而第二个"if"情况确实非常有效,为什么?
之间有什么区别
char *key_str="kiwi";
Run Code Online (Sandbox Code Playgroud)
和
char *key_str = strdup("kiwi");
Run Code Online (Sandbox Code Playgroud)
例如:
int strCmp(void *vp1, void *vp2) {
char * s1 = (char *) vp1;
char * s2 = (char *) vp2;
return strcmp(s1, s2);
}
Run Code Online (Sandbox Code Playgroud)
为什么这些*key_str在函数中使用时表现不同strCmp()?
源代码:https : //github.com/alexparkjw/typing/blob/master/pa2.c