【发布时间】:2021-08-12 16:14:31
【问题描述】:
编辑:编辑了所有内容,抱歉造成误解。
我正在尝试编写一个函数来在字符串中查找字典序上最大的单词。
一个词的定义是这样的:包含一个字母 - 可以使用 isalpha() - 不包含空格 - 可以使用 isspace()
如果 s = "He llo wor l d"
s 包含以下单词:he、llo、wor、l、d。
要查找两个字符串之间的字典顺序最大的单词,我可以使用 strcmp。
函数原型是:char *biggestWord(char *s),它应该返回字典序上最大的单词。
我确实坚持了好几个小时。这是我尝试做的:
我不知道下一步该做什么。如果连续有两个空格,我的算法想法甚至都行不通。
char *biggestWord(char *s) {
//We will find the first string and compare it to each one of the new strings
//We will keep the value of the higher string everytime
char *res;
char *temp;
int indexStart = 0; //Will contain the index of the first character to then store on temp
int indexEnd = 0; //Will contain the index of the last character to then store on temp
for(int i = 0; s[i] != '\0'; i++) {
if(isspace(s[i])) {
indexEnd = i - 1;
temp = myStrCpy(s, indexStart, indexEnd); //Will extract the string using start and end index, and put it into temp
indexStart = i+1;
}
}
}
【问题讨论】:
-
“我的目标是使用 strcmp() 找到最大的 ASCII 字。” - 没有理由使用
strcmp来搜索最长的非空格,带有终止字符串的字母数字字符。所以这从一开始就是一个兔子洞。 -
@WhozCraig 不是最长的序列,我在用 strcmp 比较所有内容时寻找最大的词(即单词中的最高 ASCII 值)。
-
@Rayan Dev 不清楚该函数应该返回字符串中最大单词的位置还是代表最大单词的字符串。
-
@Rayan Dev 你不能应用函数 strcmp。
-
@VladfromMoscow 我需要将字符串本身作为指向 char 的指针返回。