0 like 0 dislike
4.3k 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

 

[Exercise] Coding (C) - asked in Chapter 13: Strings by (12.1k points)
ID: 41439 - Available when: Unlimited - Due to: Unlimited

edited by | 4.3k views
0 0
12345

27 Answers

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

char prefix(char s1[], char s2[])
 {
 int i;
 int length;
 int count=0;
 
* * **** **** **
 
** ** * ***** * * *** **** * * * ** ** *
* * **** ** *** * *****
* * *** ** * * ** * * * *** *** ** ** == s2[i])
* *** **** * ** * ****** * * ** * * * **** **
* * * **** * ** *** * ** * * *** * *** * * * ** ****** ****
* * **** * * * *** * ** * * * ****** ** *
 
* * * * ** ** ********* *
 
* ** * *** *** *** * * ** *** * * == length)
***** ** * * * ** **
* * **** ** * * ***** *** * * * ** * * ** ** * * * is a prefix of %s",s1,s2);
* * **** * **** ** ** * *
 
* * ** *** * * * ** * ** * *** **
* * * * ** * **** ** * * * ***
* * * * * ******** * ** ** * ***** *** * * **** * is not a prefix of %s",s1,s2);
* ** **** **** ** ** *
 return 0;
*** ****** ** ** * * **
 }

int main(void)
{
* * ********* ** ** * *** s1[100];
* * **** * * ** * ** * s2[100];
* * * * ** * * *** ** *** i,j;
**** * * * ** ** * * ***
* * * ** **** ******* * * ** * *** * s1);
* * * *** * ** *** *** * * * ** * * *** ** s2);
* * ** ** ** ** * * *** **
* ** ** * ** **** * ** *** *** * *
* ***** * ***** *
* * ** *** * **** * * 0;
}
answered by (-193 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include **** ****
#include * * ** * * **** *

int prefix (char st1[],char st2[]){
* ** ** ** * ** ** * * * len=0, i, cnt=0;
**** ** * ** * *** ** * *
** ** *** * * * *** * = (unsigned)strlen(st1);
* * * ** * * * **
* ******* ** * * * ** (i=0;i<len;i++)
* *** * ** * * * * **
**** ** * (st1[i]==st2[i]){
* ** * ** * * * * ** * * ** * * * ***** *** *** ** * *****
* *** ** * * * * ** * ** * * ** *
*** * * * ** * ******
*** ****** * * * * ** (cnt == len){
* ** * * * ** ** * ****** ** *** * * * ****** * * ***** 1;
** * * * * *** ***
** * * * ***** *
** *** *** ** ** ** *** 0;
* **** ***** * **** **
}

int main(void) {
** * ** * ** ** ** *** st1[256];
*** * **** **** * * ***** st2[256];
* * ** * ****** * **** * * ** ** *** * ** * ** st1);
* * **** * * * **** **** ** ** * ****** st2);

* * * * * ****** (prefix(st1,st2)){
** ** ***** ** * * ** **** * * *** * ** * * * ** is a prefix of %s", st1, st2);
**** ** *** **** * * **
* ** * * * ** * ** * * ***
* ** * * *** *** *** * * *** * ** ** * * ***** ** * is not a prefix of %s", st1, st2);

** *** ** * * *** * * ** * 0;
}
answered by (323 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<string.h>

int main()
{
    int i,j;
    char a[100], b[100];
** * *** * ** ** * * ** **** * * *
    scanf("%s",b);
    j=strlen(a);
    for(i=0;i<j;i++)
* ** ** * * **** **** * ** ** ** * * **
* * * **** * * * * ** ***** ** ** * *** *** ** ** *** ***** **** ***
* *** * **** * ** * * * ** ******* *** * **** ** ** ** **** * * * ** * *** * *
* ***** * * *** *** * ** *** ***** *** * * ** * *** ** ***** ** * * * ** ** ** * ** * * *** * * ** * ** ** * * ** is not a prefix of %s",a,b);
** ***** ** **** ** * * * ** * * ** * ** ** ** * * * * ** *** * * *** ** * ** * ** * ***** * *
** * ** * ***** **** ** ** ** *** * * ***** * * * ******* ** ** * * ** ***
* * *** * * * ** ***** ** ** ** * *** * *** * *
**** ** * ** ******* * * *** ****** ***** *** ** * * * * ** ** ********* * *
** **** * * *** *** ** * ****** * ******* ** * * ** * * *** * * * * ** * ** ** * * ** ** ** *** * * 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 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>

int main(void)
{
    char pre[100], str[100];
    int i;
** * ** ** * * ** *** * **** * * * *
* **** * *** * * * * * ** *** **

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

* * *** ** ** ** 0;
}
answered by (-281 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: Correct output
Case 2: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
0 0
-----------Re-judge-----------
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 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()
{
**** ***** ** ** * * a[100];

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

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

* *** * ***** * * * prefix = 1;

* * ** * * ***** * * * ** * * * * ** a, b);

* *** * * * * *** * len = strlen(a);

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

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

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

*** ** *** * * ***
}
answered by (-285 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include ** ** ****
#include * * * *

int main()
{
* * * **** *** * * ** a[100],b[100];
** *** * * * * ** ** i,j,prefix = 1;
*** *** ** * * ** ** * * %s",a,b);
* ** ** * ** ** * * len = strlen(a);
* *** *** **** ** ** **** = 0 ; i < len ; i++)
****** * * ****
** * **** * * * * * ** * *** * ** * * * * ** * * * *** != b[i])
** *** * ** * ** * ** * *** * * ** * ** * *
* *** ***** * ** * * ** **** * ****** *** * * ** * ** ** ** ** ** ** * * = 0;
** * ** ** ** **** *** ** * **** *** * * * * ** ** * * * * * * is not a prefix of %s",a,b);
**** * ** * ** ** * * **** * ** ***** * * ** * ***
* ** *** * * * **** **** ** ** **

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

 
** ** * ***** ********* * * 0;
}
answered by (-127 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
0 0
prog.c: In function 'main':
prog.c:8:5: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
     scanf("%s %s",a,b);
     ^~~~~
prog.c:8:5: warning: incompatible implicit declaration of built-in function 'scanf'
prog.c:8:5: note: include '<stdio.h>' or provide a declaration of 'scanf'
prog.c:15:13: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
             printf("%s is not a prefix of %s",a,b);
             ^~~~~~
prog.c:15:13: warning: incompatible implicit declaration of built-in function 'printf'
prog.c:15:13: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:21:9: warning: incompatible implicit declaration of built-in function 'printf'
         printf("%s is a prefix of %s",a,b);
         ^~~~~~
prog.c:21:9: note: include '<stdio.h>' or provide a declaration of 'printf'
0 0
prog.c: In function 'main':
prog.c:9:11: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     int n=strlen(a);
           ^~~~~~
prog.c:9:11: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:9:11: note: include '<string.h>' or provide a declaration of 'strlen'
0 0
prog.c: In function 'main':
prog.c:9:11: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     int n=strlen(a);
           ^~~~~~
prog.c:9:11: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:9:11: note: include '<string.h>' or provide a declaration of 'strlen'
0 0
prog.c: In function 'main':
prog.c:8:5: warning: implicit declaration of function 'scanf' [-Wimplicit-function-declaration]
     scanf("%s %s",a,b);
     ^~~~~
prog.c:8:5: warning: incompatible implicit declaration of built-in function 'scanf'
prog.c:8:5: note: include '<stdio.h>' or provide a declaration of 'scanf'
prog.c:15:13: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
             printf("%s is not a prefix of %s",a,b);
             ^~~~~~
prog.c:15:13: warning: incompatible implicit declaration of built-in function 'printf'
prog.c:15:13: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:21:9: warning: incompatible implicit declaration of built-in function 'printf'
         printf("%s is a prefix of %s",a,b);
         ^~~~~~
prog.c:21:9: note: include '<stdio.h>' or provide a declaration of 'printf'
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include ** * *****
#include ***** ** *** *

int main()
{
* * * ** ** * ** a[100],b[100];
* * * * ** ** * ** ** *** * i,j,prefix = 1;
** * ****** * ** ** **** ** **** * %s",a,b);
**** * ** * ** ** *** len = strlen(a);
* **** **** ** *** ** = 0 ; i < len ; i++)
* *** ****** * ***
***** * * * ** ** * * * * ** * ** != b[i])
* * ******** * * ** *** * * * ** * ** ** * **
* ** ***** * * * * * * ** ** * *** * * * ******* * **** * ** * = 0;
**** * ** *** *** * ** * ** ** *** * ** ** ******* * * ** * ** * ** * * * * * is not a prefix of %s",a,b);
** *** * *** ***** ** ** * * * * *** ** ** * *** * * * *
** ****** *** * *** * * * * * * * * * **** *

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


* * * ** ** ** *** * 0;
}
answered by (-255 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include * ** * **
#include ** * * ** * *

int prefix(const char *pre, const char *str)
{

** * ** *** * *** ** * *** * ******* *
** * * ** ** ** ** * * * * * ** *** * ** 0;
*** * * *** ** ** ** **** **** **
* * * * ** **** * * ** * * * ** ** * * ** * 1;
* * * ** * * ******** * * return 0;


}

int main()
{
** ****** * * * *** * * pre[100],str[100];
***** ** *** *** *
* * ** * * *** *** * ** **** ** * * *
* * ** * ***** **** * * **
* ** ***** * *** * * *** * * ** * ** * * * * ** * * is a prefix of %s",pre,str);
** ** *** * ** * ** ** ***
** ** *** * * * **** **** *** ** ******** * * * ** * is not a prefix of %s",pre,str);
** * ** ** * * ******* ** 0;
}
answered by (-196 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 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
-----------Re-judge-----------
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 perfix(char *a,char *b){
* ** *** * ** * * *** c;
** * * ****** * *** ** ** **
** ** * * * ** ** ** * * *** * *** * * ** * * ** 0;
*** * *** ** * * ** ***
*** ** * *** * * * *
** *** * * * ****** * ***** ** * * * ** ** * * ** * *** *** of a string
* * ** ** *** ** *** ** * * ** ** * *** * * ** ** * ****** * * **
* * ** * * ** *** ** * **** ** * ** * ** * * * ***** * * ** * * ***** * 0;
* **** * **** * **** * ** * *** * *******
** *** * ** * **** *** ** * **** *** **** 1;
    }
}

int main(){
** * * * ** * *** ** a[100],b[100];
* * * * * ** * * ** * * * * ** %s",a,b);
* * ** ** * ** * ** * ***
*** ** ** * ** ** * *** * *** *** * ** * ** * ** * * **** ** is a prefix of %s",a,b);
**** * ** * ** * ** *
* ** * ** *** ***** * ** * ***** * ** * * is not a prefix of %s",a,b);
*** * ** * ** *** * * 0;
}
answered by (-498 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct 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()
{
***** *** * *** * * ** ** p[100],s[100],a[100]={};
* **** *** ** ****** * ** *** t;
* * ** * *** * ****** **** * **
** *** * * * **** * * ** * * ** **
**** ** ** * * * *** * ** * * ** %s",p,s);
* * ** * * ** * * *** **
** ** ***** **** * ** *
* *** *** * ** * ** * *
* * ***** * * *** *** *
** **** **** * * ** * * ** * ** ***
* * * ** ** * * * ** * ***** * ** * * ** * * *** * *** is a prefix of %s",p,s);
* * * * *** * *** *** ** **
**** * ** ***** ***
** ******** ** * * ******* ** ** * * ** * ** * is not a prefix of %s",p,s);
** *** * ** ***** * * * *
* * ** *** * * * ** ** * 0;
}
answered by (54 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.179.136
©2016-2024

Related questions

0 like 0 dislike
57 answers
[Exercise] Coding (C) - asked Jan 11, 2018 in Chapter 13: Strings
ID: 41431 - Available when: Unlimited - Due to: Unlimited
| 5.7k views
0 like 0 dislike
29 answers
[Exercise] Coding (C) - asked Jan 11, 2018 in Chapter 13: Strings by thopd (12.1k points)
ID: 41436 - Available when: Unlimited - Due to: Unlimited
| 4k views
0 like 0 dislike
53 answers
[Exercise] Coding (C) - asked Jan 4, 2018 in Chapter 13: Strings by semicolon (5.2k points)
ID: 40715 - Available when: 2018-01-04 18:00 - Due to: Unlimited
| 5.3k views
12,783 questions
183,443 answers
172,219 comments
4,824 users