1 like 0 dislike
17.5k views

A string p is a prefix of a string s if the leading characters of s are identical to p. For example, "abc" is a prefix of "abcde". Develop a function prefix that checks if a C string p is a prefix of another C string s.

p是s的前綴若s開頭的一段字串等於p。例如,"abc"是"abcde"的前綴。寫一個程式 判斷字串p是否字串s的前綴。

Example input 1:

abc
abcde

Example output 1:

abc is a prefix of abcde

 

Example input 2:

bc
abcde

Example output 2:

bc is not a prefix of abcde

 

[Exam] asked in Final Exam
ID: 42299 - Available when: 2018-01-17 14:00 - Due to: Unlimited

retagged by | 17.5k views

37 Answers

0 like 0 dislike
Hidden content!
#include *** * *** ** *
#include * * * *
******* ** * **
* **** ** * ****

int prefix(char a[], char b[]){
int i, j,n;
int cnt=0;

n= * ** * *

** * ** * * * * * ** * *
* * ** ** * * * **** * * * ** * ** == b[i]){
** ** *** **** * ** ** * ** * *** * *
* ***** * * **

* ** * * ** ****
if (cnt == n){
* * * * * *** * ** * ** * * ** ** **** * 1;
* ** * **** ** * ** * ** ** ** * ***
* *** * * *** ** ** * *** *
* * **** * * * ** ** ** * ** *** ***** * * * 0;

}

int main()
{
* * ** * **** ** * ** s1[100], s2[100];
* *** * **** *** i,j;

* * **** ** ***** *** ******** * s1);
* ** * ** *** * *** *** ** ** * s2);
***** * ** * (prefix(s1, s2)){

** ** ** * * * * * * ** ** * *** * * ** ***** is a prefix of %s", s1,s2);
* ***** * * ** ** ** * ** * * * * ** *
* * *** *** * ** * * ****** * * **
** *** *** ***** *** ** ******** * * * * *** * * *** **** * * is not a prefix of %s", s1,s2);



}
answered by (323 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>

int main(){
** ** * * ** ** * * ** c;
**** ******* ****** * a[100],b[100];
*** ***** * ** *** * **** ** * %s",a,b);
* *** *** * * **** * * ***** ***** *
** *** ** ** ** ** ** ******* ** * * * * **** * 0;
* * ***** ** ** * ** * %d",strlen(a),strlen(b));
** ** * * * ** ** ** * ** * ** **** * *
* ** *** ** * * *** * * ** ** * * * * ** * * ** ** * **** * *
*** * * ***** * *** ********* *** ***** * ** ** * * ** **** * * * * ** *** ** *** * * ** * is not a prefix of %s",a,b);
** ** * *** * * *** ** **** *** * * * *** *** ** * * * ** * * * ** *** * * *** ** ** **
** ** * ** ***** * * *** *** * ** * * * * * ** * ** * * * ** * ** * *
* ***** * ** * *** * ** * *** ** * * **** * ** *** if(c==strlen(a)-1)
* ** ** **** * * * ** * ** * * * **** * * ** * ** * * ** *** ** *** * * * ** * ** is a prefix of %s",a,b);
    }
** **** * ** ** * * * * 0;
}
answered by (-498 points)
edited by
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>
int main()
{
* * * * * * *** * ** n,count;
*** *** *** ** * *** * *** * ****
* ** ****** ** * * *** * **** ***** %s",a,b);
*** * ** ** ** * * *

* **** * * ** * *** ** * * ** ** * ** ** *
* ***** ** *** * *** ***
* *** * * ** *** * * * * ** ** * ** * * ** * * **
* **** * * * * *

* * * * * * * ** * ** (strcmp(a,c)==0)
**** ** * * ** * **
* ** * ***** * *** **** ** ***** * **** ** * is a prefix of %s",a,b);
*** ** * * * ** *
** * ******** ** * *
* ** ** ** *** ****
* * * ** * **** ****** * * * ********** ****** **** is not a prefix of %s",a,b);
** * * ** * * ** ******
*** ** * ** ** * *** * * 0;
}
answered by (-284 points)
edited by
0 0
prog.c: In function 'main':
prog.c:8:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s %s",&prefix,&word);
             ^
prog.c:8:16: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[100]' [-Wformat=]
     scanf("%s %s",&prefix,&word);
                ^
prog.c:10:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     for(i=0;i<strlen(prefix);++i)
               ^~~~~~
prog.c:10:15: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:10:15: note: include '<string.h>' or provide a declaration of 'strlen'
prog.c:18:6: error: expected '(' before 'printf'
      printf("%s is a prefix of %s",prefix,word);
      ^~~~~~
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
int main()
{
** ** *** ****** ** * ** *** a[100],b[100];
***** ** ****** ***** ****** * * * * * **** *
    int l1,l2;
* * ** ******** * **** *******
* * * * * ****** *** * *
* * * * *** * * * * **
******* ** ** * ** * * ** * ** * ***** * ***** is not a prefix of %s",a,b);

    }
********* ****** * ** * if(l1<=l2){
*** * ***** * ** *** *** *** * * ****** prefix=1;
** *** * **** ***** * * ** * *** * ** *** ********* i=0;
* * * *** *** ***** ** ** *** ********* * ***** **** * * * **
* * ** * ** * * * **** ** * ** * * * ** * ** **** ******* (a[i]!=b[i]){
* * ** * **** ** * * * * * * * *** * ** * ****** ******* **** * ** **** ** ** *** * * ** ** **
** * * * * ** **** * ** * * * * * **** * * * * ***** **** * ** * *** **** *** ** **** * *
* * * * * ** * **** * ** * * **** * ** * ***** ** ***** ***


*** * * ********* * ** ***** * ** ** **** *
****** ****** * ** *** * *** * * * * * *** * *
* * *** * ** **** ** * *** ** * * * * * * * **** *** * **** * * * ** ** * ** *** is not a prefix of %s",a,b);
* * ** ** *** * * * * * ** * *** ***
* * ** * ***** * *** *** * * ** * ***
* * ** * * * * *** ***** **** ***** ** * ** * * ** is a prefix of %s",a,b);


    }


}
answered by (-329 points)
0 0
prog.c: In function 'main':
prog.c:5:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s%s",&a,&b);
             ^
prog.c:5:15: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[100]' [-Wformat=]
     scanf("%s%s",&a,&b);
               ^
prog.c:7:8: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     l1=strlen(a);
        ^~~~~~
prog.c:7:8: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:7:8: note: include '<string.h>' or provide a declaration of 'strlen'
0 0
prog.c: In function 'main':
prog.c:6:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s%s",&a,&b);
             ^
prog.c:6:15: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[100]' [-Wformat=]
     scanf("%s%s",&a,&b);
               ^
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
* * **** *** ** * *
int main(void)
{

***** * ** * * * pre[100];
**** * * *** * *** word[100];
* * * * ** **** * *** * %s",pre,word);
** *** * ** * * * * * * * * **
*** * * ** * ** * * ** * *****

* * ** * * *** * * *** i;
* * *** **** ** * *********** ** **** *
* * * ****** ***** * ** *
** **** * ** * * * * ** ***** * * **** * * *
** * ** ** * * *** ** ** **** ** * * * ** *** * ***
** * ** ** *** * *** * * ** ** * * * **** *** ** **** * * ** ******** * ** * is not a prefix of %s",pre,word);
*** * * * * *** * * * * ***** ** ***** ** ** ** 0;
** * * * * ***** * ** ** ** *** * * ** *

*** * * * * * ** * * *
printf("%s is a prefix of %s",pre,word);
return 0;
}
answered by (-249 points)
0 0
prog.c: In function 'main':
prog.c:8:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s",&pre);
             ^
prog.c:9:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s",&word);
             ^
0 0
prog.c: In function 'main':
prog.c:8:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s",&pre);
             ^
prog.c:9:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s",&word);
             ^
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main ()
{
*** *** * * ** * ***** * pre [100],a[100];
* ***** * **** * ** n ,i;
* * ** * *** * ** * * ******* ** ** * *
**** *** * ** * ** *** * ** *
** ** **** * *** *** * * * **** ** * *
** ** *** ** * * * ** * *
* * **** * * * * * * * *** * * **** (n=0;n<strlen(pre);n++)
*** **** * * * ****** * * * ** * * **
* ** * * ***** ** * * *** * **** * ** *** * * **** ** ** * (pre[n]==a[n])
** *** ** ** ** ** * * * * * * **** *** ** * * * * *** ** ** * *** * ** ** is a prefix of %s",pre,a);
* * *** * * * *** **** *** ** * ** * ** ***** * * * **** * * * ***
*** ** * **** * ** *** ** ** * * ** * **** * * ** * * ** ** * * ** * ** * * *** * * ** * is not a prefix of %s",pre,a);return 0;
*** * ** ***** ** *
** * * **** * * * ** *** ** ** ** ** * * ***** *** * * ** * ** *

*** * * ** * ** ** 0;
}
answered by (-32 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include ** * **
#include * ** ****

int main()
{
**** *** ** * * * a[100];

* ** ** * * ** ** ** b[100];

* **** * * *** * * *** ** ** * %s", a, b);

*** *** * ** * * *** ** * len_a = strlen(a);

*** ***** *** * **** * **** i = 0;

* * ** * * *** ** *** * flag = 1;

** *** * ** * ** ** * * *** = 0; i < len_a; i++)
* * ******* * * *
**** ***** **** * * * * ***** ** * * ** **** != b[i])
** ** * * *** * ** * ** ********* **** *
** * * ***** ** *** *** ** ** * ** * * * * ** * * ** *** * ** * *** = 0;
* * ** * ** ** * ** *** * * * **
* **** **** ** *** *

* ** * *** *** * == 0)
****** *** * * *** ** *
* * ** ** ** ******* * **** * * * ** * * * * is not a prefix of %s", a, b);
* * * * ****** ** * **
* * * * * **** *
* * ** *** * * * *
***** ** ********* *** * ** * * * **** *** ** *** ** is a prefix of %s", a, b);
* * ** * ** ** * * * *
**** * *** * * * ** *** * 0;
}
answered by (-285 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
* ***** * ****** * **
int main()
{
** * * * * ***** ***** ** i,n;
* ***** ** **** * ** ** p[100],s[100],a[100]={};
**** * * **** * **** ** * * %s",p,s);
*** * * ** **** * *** ** ** * * *
* ** ** ** * ******* ** *
* * ** * ******* * * * ** ** * * **
* * *** ** *** * **** * ******* *
* * * ** ** ***** * ******** * * * **** *** * ** ***** is a prefix of %s",p,s);
*** * ** *** **
********* *** *** * * ** *
** ** * * * * * *** * * **** * **** * ***** ** * *** * * is not a prefix of %s",p,s);
*** *** ***** * **
** * ** * ** * *** ***** * * ** *** * ***** ** * *
* ** ***** ** ****** * ** *** 0;
}
answered by (54 points)
edited by
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>

int main()
{
    int i;
    char a[100];
    char b[100];
** * **** * * ** ** * ** * * ****** **
    scanf("%s",b);
    for(i=0;i<strlen(a);i++)
*** *** * * * ***** *** * * * * ** * *
** **** **** ** ** * * * * *** ** ****** * ** ** ***** * **** *** * ** ** != b[i])
** ** *** * *** ** ** * * ****** * * ** * * ****** ** * * ** * ** ** * * * *** * * ** **
**** * ** * ** ** *** * * * *** ** *** ** ** * ***** *** ***** ** * **** * * * * ** * ** * * * *** * * ** is not a prefix of %s",a,b);
*** *** **** ** ** ** ** * * * ** *** ** * * *** * ** * * **** *** ***** * ** ** ** ** ** * **** * * * ***** **
** * *** ** *** **** ** *** ** ** ** * * *** ** * * *** * ** ****** *** ** ** ** *** * * *
*** * ****** * * **** *** * **** * * ** * ** * * *** * *** * * * ***** * == b[strlen(a)-1])
* ** ** * * * * *** * ** * * * ****** ** * * *** * * * ** * ** *** * ** *
* *** * *** ** * * ** *** * * * * *** * **** *** ** * * **** * ** * ** * * **** ** * * ** * ** * * is a prefix of %s",a,b);
* ** * * ** * *** ** ******* **** * * * * **** * * ** * *** *** ** * **** * * * ** * ******* * * * ** *
** *** * * **** *** *** ** ** ***** **** ** ** * ****** ***** * * ** **** * *** * ** *
** *** * * ** *** * * ** *** ** * * * * *
}
answered by (-301 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define n 100

int aa(char a[],char b[]){
** ***** ***** * * * **** * * i;
*** ** **** ** * * * (a[0]!=b[0]){
********** * * * ** ** * * * ** * *** ** * 0;
** ** * ***** * ** *
**** ** * * **** * * * * * *****
* * * ** * * ** * ** * * ** ** ** * ** * *** a[i]==b[i])
* * ** * ** *** * * ** * *** * * *
** * * ** * * ** *** * * * ** * * ** ** * * * ** * * ** * 1;
*** * * **** * *** * ** * ** ** *********** * * * * *** ** ** * *** * ** **
* ***** * * *********** ***** * * *
** ***** ****** ***** ** * **
** * **** * * *** ** * *** * 0;

}

int main(){
*** * *** * * ****** **** * c[n],d[n],e[n];
**** **** * *** * ***** i;
* * **** ** * ** * * * ***** * %s",c,d);

*** * *** * * ***** *
* ** *** ***** * *** * *** ** * * **** *** is a prefix of %s",c,d);

* ** * **** * *** * ***
** * * *** ******** **** ******* * **** *** * ** *** **** is not a prefix of %s",c,d);


}
answered by (-32 points)
edited by
0 0
Case 0: Wrong output
Case 1: Correct output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.243.109
©2016-2026

Related questions

1 like 0 dislike
37 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42302 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 16.1k views
2 like 0 dislike
31 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42301 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 15.1k views
1 like 0 dislike
32 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42300 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 18.9k views
2 like 0 dislike
30 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42298 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 13.1k views
0 like 0 dislike
4 answers
[Exam] asked Jan 19, 2018 in Final Exam
ID: 43460 - Available when: 2018-01-20 09:00 - Due to: Unlimited
| 3.4k views
12,783 questions
183,442 answers
172,219 comments
4,824 users