C困扰我对弦乐的处理.我心中有这样的伪代码:
char *data[20];
char *tmp; int i,j;
for(i=0;i<20;i++) {
tmp = data[i];
for(j=1;j<20;j++)
{
if(strcmp(tmp,data[j]))
//then except the uniqueness, store them in elsewhere
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我对此进行编码时,结果很糟糕.(我处理了所有内存,小东西等)问题显然在第二个循环中:D.但我想不出任何解决方案.如何在数组中找到唯一的字符串.
输入示例:abc def abe abc def deg输入唯一的输入:应找到abc def abe deg.
我编写了一个函数模板和一个显式专用的模板化函数,它只需要3个参数并计算其中最大的参数并打印出来.
专用函数导致错误,而模板工作正常.但我想使用char*类型.
这是我得到的错误=>
error: template-id ‘Max<>’ for ‘void Max(char, char, char)’ does not match any template declaration
以下是我的代码:
template <typename T>
void Max(T& a,T& b,T& c)
{
if(a > b && a >> c)
{
cout << "Max: " << a << endl;
}
else if(b > c && b > a)
{
cout << "Max: " << b << endl;
}
else
{
cout << "Max: " << c << endl;
}
} …Run Code Online (Sandbox Code Playgroud) 我需要逐行搜索文件中的两个特定单词,如果它们存在,则打印"Found!".
这是file.txt(有四列)
bill gates 62bill microsoft
beyonce knowles 300mill entertainment
my name -$9000 student
Run Code Online (Sandbox Code Playgroud)
以下是我的想法,但它似乎不起作用
char firstname[];
char lastname[];
char string_0[256];
file = fopen("file.txt","r+");
while((fgets(string_0,256,file)) != NULL) {
//scans the line then sets 1st and 2nd word to those variables
fscanf(file,"%s %s",&firstname, &lastname);
if(strcmp(firstname,"beyonce")==0 && strcmp(lastname,"knowles")==0){
printf("A match has been found");
}
}
fclose(file);
Run Code Online (Sandbox Code Playgroud)
请帮忙.可能是指针没有移动到while循环中的下一行吗?如果是这样,我该如何解决?
由于未知原因,运行我的C程序的结果是非常意外的.我认为它必须是某种初学者的错误,但我真的找不到,它在哪里.
#include <stdio.h>
#include <string.h>
int main()
{
char string1[50];
char string2[50];
int compare;
puts("Enter two strings: ");
fgets(string1, strlen(string1)+1, stdin);
fgets(string2, strlen(string2)+1, stdin);
compare=strcmp(string1, string2); /* usage of extra variable makes the code more readable but wastes more memory */
printf("%d: ",compare);
if (compare<0) puts("First string is lesser");
else if (compare>0) puts ("First string is bigger");
else puts("Strings are equal");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并在测试:
Enter two strings:
heheisntthisalongstring
heheisntthisalongstring
1: First string is bigger
------------------
(program exited with code: …Run Code Online (Sandbox Code Playgroud) 根据我的理解,strcmp()(没有'n'),在任一参数中看到空字符时,立即停止处理并返回结果.
因此,如果其中一个参数已被100%确定地知道为空终止(例如,它是一个字符串文字),那么使用strncmp()(带有'n')调用strlen()作为第三个的一部分没有任何安全性好处将比较限制为已知字符串长度的参数,因为strcmp()它将永远不会读取比已知终止字符串中更多的字符.
事实上,在我看来,调用strncmp()其长度参数是strlen()前两个参数中的一个是唯一的strcmp(),因为它通过评估strlen()表达式来浪费时间线性的已知终止字符串的大小.
考虑:
示例代码A:
if (strcmp(user_input, "status") == 0)
reply_with_status();
Run Code Online (Sandbox Code Playgroud)
示例代码B:
if (strncmp(user_input, "status", strlen("status")+1) == 0)
reply_with_status();
Run Code Online (Sandbox Code Playgroud)
前者对后者有什么好处?因为我在其他人的代码中看到了很多.
我对这些功能的工作原理有缺陷吗?
我有两个字符串
字符串1:"sebastien"
字符串2:"塞巴斯蒂安"
我想通过忽略é(Accents)字符来比较这两个字符串.谁能知道这个逻辑吗?
提前致谢
我的程序需要以下功能:
注意:我没有包含数字1,2和4的代码,因为我已经完成了它们.第三个是我的问题.
我的问题是如何将搜索输入与s_college中的用户输入进行比较以获得学生数量.我知道的唯一方法是使用strcmp(),但它给了我这个错误:从'char'到'const char*'的无效转换[-fpermissive]
那么如何比较这两者以获得每所大学的学生人数呢?
#include<stdio.h>
#include<string.h>
#include<conio.h>
int i,n,sum,search,num=0,ctr=0;
char answer,choice,choice2,search2;
struct record{
int s_id;
char s_name[100];
char s_course;
char s_college[5];
int s_scoress;
}id[100],name[100],course,college[100],scores;
struct s_scores{
int frst_grade;
int scnd_grade;
int fnl_grade;
}first,second,final;
void ADD();
void COLLEGE();
void ID();
void COLLEGE(){
printf("Enter college (abbreviation only)");
scanf("%s",&search2);
for(i=0;i<num;i++){
if(strcmp(college[i].s_college,search2)==0);
ctr++;
}
printf("The number of students in %s is %d",search2,ctr);
Run Code Online (Sandbox Code Playgroud) 这是我在glibcstrcmp中找到的函数:
int
STRCMP (const char *p1, const char *p2)
{
const unsigned char *s1 = (const unsigned char *) p1;
const unsigned char *s2 = (const unsigned char *) p2;
unsigned char c1, c2;
do
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (c1 == c2);
return c1 - c2;
}
Run Code Online (Sandbox Code Playgroud)
这是一个非常简单的函数,其中 的主体以和的值while启动and ,并继续直到是或和的值相等,然后返回和之间的差。c1c2*s1*s2c1nulc1c2 …
我有一个我自己的strcmp版本,看起来像这样
int strcmp(char str1[], char str2[])
{
int i = 0;
while ((str1[i] == str2[i]) && (str1[i] != '\0'))
{
i++;
}
if (str1[i] > str2[i])
return 1;
if (str1[i] < str2[i])
return -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的测试用例是
char a[20];
char b[20];
b[0] = 'f';
a[0] = 'f';
cout << strcmp(b, a) << endl;
Run Code Online (Sandbox Code Playgroud)
但是,我输出1,意味着它们彼此不相等.如果我在函数调用中交换a和b的位置,我得-1.当我的char都是'f'时,我不确定为什么我无法在比较中得到0返回.我觉得这是如此基本,我不知道为什么我的比较是关闭的
str1[i] > str2[i]
Run Code Online (Sandbox Code Playgroud) 我无法使用 strcmp 在 GDB 中创建条件断点:
break x if strcmp(str.c_str(), "foo") == 0
Run Code Online (Sandbox Code Playgroud)
你为什么问?
因为:
print strcmp("hello", "hello")
Run Code Online (Sandbox Code Playgroud)
产量
(int (*)(const char *, const char *)) 0x7ffff76ffe70 <__strcmp_sse2_unaligned>
Run Code Online (Sandbox Code Playgroud)
即使将其转换为整数:
print (int)strcmp("hello", "hello")
Run Code Online (Sandbox Code Playgroud)
它返回一些无意义的值,如 -143655312
这是“解决”我的问题的一种不太优雅的方法。我可以在自己的代码中定义一个函数:
int mystrcmp(const char *str1, const char* str2){
return strcmp(str1, str2);
}
Run Code Online (Sandbox Code Playgroud)
现在我可以使用这个函数来代替我的条件断点。但这不是真正的调试,是吗?当您必须更改原始代码以进行调试时,您就输了!
那么我错过了什么?