Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编写一个函数any(s1,s2),返回字符串s1中出现任何字符的第一个位置 /*程序返回字符串s1中出现任何字符的第一个位置,或者如果s1中不包含任何字符,返回-1*/ #包括 #包括 内部主(空) { char s1[]=“这很有趣”; 字符s2[]=“fin”; int loc=关闭(s1,s2); printf(“%d”,loc); printf(“\n”); 返回0; } int off(字符s1[],字符s2[] { int i=0; int loc=-1; 而(s1[i]!='\0') { int j=0; 而(s2[j]!='\0') { 如果(s2[j]==s1[i]) { loc=(int)s1[i]; 返回loc; } j++; } i++; } 返回loc; }_C - Fatal编程技术网

编写一个函数any(s1,s2),返回字符串s1中出现任何字符的第一个位置 /*程序返回字符串s1中出现任何字符的第一个位置,或者如果s1中不包含任何字符,返回-1*/ #包括 #包括 内部主(空) { char s1[]=“这很有趣”; 字符s2[]=“fin”; int loc=关闭(s1,s2); printf(“%d”,loc); printf(“\n”); 返回0; } int off(字符s1[],字符s2[] { int i=0; int loc=-1; 而(s1[i]!='\0') { int j=0; 而(s2[j]!='\0') { 如果(s2[j]==s1[i]) { loc=(int)s1[i]; 返回loc; } j++; } i++; } 返回loc; }

编写一个函数any(s1,s2),返回字符串s1中出现任何字符的第一个位置 /*程序返回字符串s1中出现任何字符的第一个位置,或者如果s1中不包含任何字符,返回-1*/ #包括 #包括 内部主(空) { char s1[]=“这很有趣”; 字符s2[]=“fin”; int loc=关闭(s1,s2); printf(“%d”,loc); printf(“\n”); 返回0; } int off(字符s1[],字符s2[] { int i=0; int loc=-1; 而(s1[i]!='\0') { int j=0; 而(s2[j]!='\0') { 如果(s2[j]==s1[i]) { loc=(int)s1[i]; 返回loc; } j++; } i++; } 返回loc; },c,C,编写一个函数any(s1,s2),返回 字符串s1中的第一个位置,其中 字符串s2中出现一个字符,如果s1出现,则为-1 s2中不包含任何字符。 例如,任何(“这很有趣”、“fin”)返回 2,('f'出现在位置8,'i'出现在位置2,和 10中的“n”),而any(“这很有趣”,“死了”)返回 -一, 方向^^ 你们看到什么问题了吗?当它应该返回8时,它返回105。我检查了ascii表,它与8 D没有关联:loc=(int)s1[I]不返回字符的位置,而是返回字符本身的ASCII值。您想要返回

编写一个函数any(s1,s2),返回 字符串s1中的第一个位置,其中 字符串s2中出现一个字符,如果s1出现,则为-1 s2中不包含任何字符。 例如,任何(“这很有趣”、“fin”)返回 2,('f'出现在位置8,'i'出现在位置2,和 10中的“n”),而any(“这很有趣”,“死了”)返回 -一,

方向^^


你们看到什么问题了吗?当它应该返回8时,它返回105。我检查了ascii表,它与8 D没有关联:

loc=(int)s1[I]
不返回字符的位置,而是返回字符本身的ASCII值。您想要返回的是
i
loc=i

loc=(int)s1[i]
不返回字符的位置,而是返回字符本身的ASCII值。您想要返回的是
i
loc=i

因此,正如我的评论和其他许多评论中所述,您返回的是字符匹配的字符值,而不是位置(i/j)。修复了下面的代码,但有一些细微的编码差异。在我看来,循环可能会让人困惑,所以使用for循环来说明它将循环,直到两个字符串的完整长度看起来更容易阅读为止

/* Program to return first location in the string s1 where any charater in a string s2 occurs or -1 if s1 does not contain any character in s2 */

#include<stdio.h>
#include<limits.h>

int main(void)
{
    char s1 [] = "This is fun";
    char s2 [] = "fin";
    int loc = theF(s1, s2);
    printf("%d", loc);
    printf("\n");
    return 0;
}

int theF(char s1 [], char s2 [])
{
    int i = 0;
    int loc = -1;
    while (s1[i] != '\0')
    {
        int j = 0;
        while (s2[j] != '\0')
        {
            if (s2[j] == s1[i])
            {
                loc = (int)s1[i];
                return loc;
            }
            j++;
        }
        i++;
    }
    return loc;
}
#包括
#包括
int any(字符s1[],字符s2[]);
内部主(空){
char s1[]=“这很有趣”;
字符s2[]=“fin”;
int loc=任何(s1,s2);
如果(loc==-1){printf(“无匹配字符”);}
else{printf(“%d\n”,loc);}
返回0;
}
int any(字符s1[],字符s2[]{
int i=0,j=0;
对于(i=0;i
正如所提到的,这正是函数所做的。使用它的一个简单实现是:

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

int any(char s1 [], char s2 []);

int main(void){
    char s1 [] = "This is fun";
    char s2 [] = "fin";

    int loc = any(s1, s2);

    if ( loc == -1 ){ printf("No matching chars\n"); }
    else { printf("%d\n", loc); }

    return 0;
}

int any(char s1 [], char s2 []){

    int i = 0, j = 0;

    for ( i = 0; i < strlen(s1); i++ ){
        for ( j = 0; j < strlen(s2); j++ ){
            if (s1[i] == s2[j]){
                return i; // i+1 depending on literal placement
                      //   vs zero indexed arrays
            }
        }
        j=0;
    }

    return -1;

}
#包括
#包括
int main(){
const char s1[]=“这很有趣”;
常量字符s2[]=“fin”;
char*ret;
ret=strpbrk(s1,s2);
如果(ret){
printf(“第一个匹配字符:%c\n”,*ret);
}else{printf(“无匹配字符”);}
返回(0);
}

因此,正如我的评论和其他许多评论中所述,您返回的是字符匹配处的字符值,而不是位置(i/j)。修复了下面的代码,但有一些细微的编码差异。在我看来,循环可能会让人困惑,所以使用for循环来说明它将循环,直到两个字符串的完整长度看起来更容易阅读为止

/* Program to return first location in the string s1 where any charater in a string s2 occurs or -1 if s1 does not contain any character in s2 */

#include<stdio.h>
#include<limits.h>

int main(void)
{
    char s1 [] = "This is fun";
    char s2 [] = "fin";
    int loc = theF(s1, s2);
    printf("%d", loc);
    printf("\n");
    return 0;
}

int theF(char s1 [], char s2 [])
{
    int i = 0;
    int loc = -1;
    while (s1[i] != '\0')
    {
        int j = 0;
        while (s2[j] != '\0')
        {
            if (s2[j] == s1[i])
            {
                loc = (int)s1[i];
                return loc;
            }
            j++;
        }
        i++;
    }
    return loc;
}
#包括
#包括
int any(字符s1[],字符s2[]);
内部主(空){
char s1[]=“这很有趣”;
字符s2[]=“fin”;
int loc=任何(s1,s2);
如果(loc==-1){printf(“无匹配字符”);}
else{printf(“%d\n”,loc);}
返回0;
}
int any(字符s1[],字符s2[]{
int i=0,j=0;
对于(i=0;i
正如所提到的,这正是函数所做的。使用它的一个简单实现是:

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

int any(char s1 [], char s2 []);

int main(void){
    char s1 [] = "This is fun";
    char s2 [] = "fin";

    int loc = any(s1, s2);

    if ( loc == -1 ){ printf("No matching chars\n"); }
    else { printf("%d\n", loc); }

    return 0;
}

int any(char s1 [], char s2 []){

    int i = 0, j = 0;

    for ( i = 0; i < strlen(s1); i++ ){
        for ( j = 0; j < strlen(s2); j++ ){
            if (s1[i] == s2[j]){
                return i; // i+1 depending on literal placement
                      //   vs zero indexed arrays
            }
        }
        j=0;
    }

    return -1;

}
#包括
#包括
int main(){
const char s1[]=“这很有趣”;
常量字符s2[]=“fin”;
char*ret;
ret=strpbrk(s1,s2);
如果(ret){
printf(“第一个匹配字符:%c\n”,*ret);
}else{printf(“无匹配字符”);}
返回(0);
}

返回的是该位置的字符代码,而不是位置本身。猜猜字符
105
代表什么。你应该检查字符匹配的i和j组合,而不是字母
loc=(int)s1[i]告诉您字母105匹配。105是
i
。作为参考,它非常接近标准库函数
strpbrk
。返回的是该位置的字符代码,而不是位置本身。猜猜字符
105
代表什么。你应该检查字符匹配的i和j组合,而不是字母
loc=(int)s1[i]告诉您字母105匹配。105是
i
。作为参考,它非常接近标准库函数
strpbrk