0 like 0 dislike
7.2k 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 | 7.2k views
0 0
12345

27 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <string.h>

int main()
{
* * *** * * ** * * * ** * n[999],str[999];
* ** * * **** * ** *** * * ** *** * ** n);
** * * ** * ***** * * * ** str);
* ** **** * **** * * * * * ** ***** * * * *******
* * * ** * **** * * ** * *** * *** * *** * ** * *** * is a prefix of %s",n,str);
*** * * * * **** **** ** *** * ** * *** ** *
** * ***** *** *** * *** * * ** * ** ** *
* * ** * ******* **** *** * ** * * ** ** * * * ** ***** *** * * ** *** * is not a prefix of %s",n,str);
* **** * *** ** *** ** ******* ** ** * * * * *** * ***
* **** ** * * *** * * * 0;
}
answered by (-249 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
-----------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
prog.c: In function 'main':
prog.c:9:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'size_t {aka long unsigned int}' [-Wformat=]
     printf("%d",strlen(n));
              ^
0 0
prog.c: In function 'main':
prog.c:9:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'size_t {aka long unsigned int}' [-Wformat=]
     printf("%d",strlen(n));
              ^
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
* ** ** * **
* ** ** ** * * *

int main()
{
* * * *** * *** * * i,plen;
* * * *** * ** ** *** *
** ** * * * ** ** ** * **** * * * * *
* ** ** * * * ** * ** * *** ** ** ********

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

** * ******* ** * ****** * * * *** * i++)
* * * ** *** ** ** * **

** *** * * ** **
*** * **
**** ******** is not a prefix of ** * **
return 0;

 }


if (i==plen-1)
*** ** **** is a prefix of * *** *

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


}
answered by (-336 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
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>

int main() {
* * ******* * ** ** ** * p[1000],s[1000];
** * * * * ** * i=0,re=0,len;

* * * ** *** ***** * * ** * ** * * *** p);
* ** * *********** * ** **** * * s);
** * *** *** * * *** * ****

* ** ** * *** ** **** ** *** * * {
* ** *** ** ****** * * * * * ** ** ***** ** ** {
**** * ***** * ** * ** * ** * *** ** * * * * ** ** ** **
*** * ** ****** * * * *** *** * * ** * **** * * * ***** *
* * *** * * ** ** ** ** * * ** ****
* * * ** *** * ****
**** *** *** ********** ** * * {
* * ***** ** ** * *** *** * **** ** ** ** **** ** * ***** * * is a prefix of %s", p, s);
** * ******** * ***
**** * * ** ****** ** *** if(re==1) {
****** *** *** ** *** * * * *** *** ****** *** is not a prefix of %s", p, s);
    }
}
answered by (-120 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
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
int main()
{

*** *** ** * * * *** * * a[100],b[100],c[100];
** * ** ** *** * * * * *** * * * ***
    int l1,l2,i=0;
** ** * *** ** *** * ****** * ***
* * * ** * * ****** * ******* *
* * * ** * * * **** * * ** *
** * *** ** ******* * * ** * **** * *** ** * ** *** * is not a prefix of %s",a,b);
    }
* ** ** *** ** * * * * if(l1<=l2){
** * ** ** *** ** * * ***** * ** prefix = 1;
** ****** **** * * ** ** * ******* ** *** * ** * * ** *
** * *** * ** ** ** ****** ***** ** ** * * * * *** * ** * * (a[i]!=b[i]){
* * ** ** * ** * * ****** * * * ** **** * **** ** * * * * * * ** * **** * ** *** * * is not a prefix of %s",a,b);
** * *** *** * **** *** * * * * * ** * ** **** * ** ** * ** ** * * * ** = 0;
** * ** * * * *** * * * *** * **** ** *** * *** ***** **** * * * * *** * * * **** * ** * **
* ** * ** * * **** * * **** ** ** ** * ***** * *** * ** ** * *
* * ** * * * *** *** ** * * *******
* * * * ** ******* ** * *** * * * ** ** * == 1){
** ** **** ***** **** * * * ** ** *** * * ****** *** * *** * * * ***** * * * is a prefix of %s",a,b);
*** ******* *** * * * * ******* * *** * ** **
    }
}
answered by (-329 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%s",&a,&b);
             ^
prog.c:8:15: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[100]' [-Wformat=]
     scanf("%s%s",&a,&b);
               ^
0 0
prog.c: In function 'main':
prog.c:7:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%s%s",&a,&b);
             ^
prog.c:7: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 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include * ** **** *
#include **** * ** **
#include * *** **** *

int main()
{
** ** * * ***** *** ** ** * prefix[100];
** * * ** ** *** * * ** word[100];
* *** * ** * ** **** ** ********** %s", prefix, word);
* * ***** **** * * * * * i;
* * * ** * ** *** = 0; i< strlen(prefix); ++i)
* * **** * ** **** *
**** * *** * ** * *** ********* * ** *** ** * * **** * * * *
* * ** **** *** *** * * **** * **** ** *** **
* **** ** * * * * * *** ** ** * ** ** *** ***** ** * ** *** **** ***** is not a prefix of %s", prefix, word);
* ** *** ****** * ** *** * ** *** ** * *** * *** ** * * * * ** 0;
* ** * * ** * * **** ** * **
** * * ** ** * **** * * **
*** * **** ** * * * * ** *** * ** * * is a prefix of %s", prefix, word);
** * **** * ** ***** * * 0;
}
answered
edited by
0 0
prog.c: In function 'main':
prog.c:15:12: warning: zero-length gnu_printf format string [-Wformat-zero-length]
     printf("");
            ^~
prog.c:17:12: warning: zero-length gnu_printf format string [-Wformat-zero-length]
     printf("");
            ^~
0 0
prog.c: In function 'main':
prog.c:15:12: warning: zero-length gnu_printf format string [-Wformat-zero-length]
     printf("");
            ^~
prog.c:17:12: warning: zero-length gnu_printf format string [-Wformat-zero-length]
     printf("");
            ^~
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include ** ****
#include * ** ** * ****** *

int main(){
**** * * * * * * * p[100],s[100];
* **** * * * ***** ** * ** ** * * *
* ** * **** ** * *** *** ** * * bf=1;
* ** * * * **** * * * * ** ****
*** ** ** * * ** * * *** * *** bf=0;
* * ** * * **** ** * **

** * * *** ****** ** **** ************ is a prefix of %s",p,s);
* * * * * ** * ***** * ** * ** * is not a prefix of **** * ***
* * * * ** * * * * ** 0;
}
answered by (-116 points)
0 0
prog.c: In function 'main':
prog.c:5:5: warning: 'gets' is deprecated [-Wdeprecated-declarations]
     gets(p);
     ^~~~
In file included from prog.c:1:0:
/usr/include/stdio.h:640:14: note: declared here
 extern char *gets (char *__s) __wur __attribute_deprecated__;
              ^~~~
prog.c:6:5: warning: 'gets' is deprecated [-Wdeprecated-declarations]
     gets(s);
     ^~~~
In file included from prog.c:1:0:
/usr/include/stdio.h:640:14: note: declared here
 extern char *gets (char *__s) __wur __attribute_deprecated__;
              ^~~~
prog.c:8:19: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     for(int i=0;i<strlen(p);i++){
                   ^~~~~~
prog.c:8:19: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:8:19: note: include '<string.h>' or provide a declaration of 'strlen'
/tmp/cclwPOj6.o: In function `main':
prog.c:(.text+0x26): warning: the `gets' function is dangerous and should not be used.
0 0
prog.c: In function 'main':
prog.c:8:19: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     for(int i=0;i<strlen(p);i++){
                   ^~~~~~
prog.c:8:19: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:8:19: note: include '<string.h>' or provide a declaration of 'strlen'
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
-----------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 main()
{
* ** *** * ** * * ** * i,count=0;
* * * ** * * ** **** * * * string1[100];
* * * * *** ******* *** string2[100];
* * * ** ****** * * ** ***** * ***
** * * ***** ** ** * ***** ** ** ** * *

* *** ******** * ** *** ****** *
* * ** * * ** ***
* *** ** * ** *** * * * ** * ** ** * ** * *****
* **** * * * *** * * * *** * *** *** * ******
**** * ** ** * ** * * ** * *** * * * *** ** * * *** **** * * ** **
*** *** * * ** * ** ***** *** * *** * * **
    }
** * ** ** * ** ** * ** * * **
* ** * **** **** **
** *** ** * * * * * * *** *** * * *** * ** is a prefix of %s",string1,string2);
** *** * * *** * ** ***
* *** * ** ** *** * * **
* *** ** *** * **
* ** *** ** * ** * * * ** * ** * *** ** * ** is not a prefix of %s",string1,string2);
*** ** *** * *
* * * * ** * * ** *** * 0;
}
answered by (-323 points)
edited by
0 0
prog.c: In function 'main':
prog.c:9:5: warning: 'gets' is deprecated [-Wdeprecated-declarations]
     gets(string1);
     ^~~~
In file included from prog.c:1:0:
/usr/include/stdio.h:640:14: note: declared here
 extern char *gets (char *__s) __wur __attribute_deprecated__;
              ^~~~
prog.c:10:5: warning: 'gets' is deprecated [-Wdeprecated-declarations]
     gets(string2);
     ^~~~
In file included from prog.c:1:0:
/usr/include/stdio.h:640:14: note: declared here
 extern char *gets (char *__s) __wur __attribute_deprecated__;
              ^~~~
/tmp/ccIBxtbQ.o: In function `main':
prog.c:(.text+0x2f): warning: the `gets' function is dangerous and should not be used.
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
-----------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.69.17.24
©2016-2025

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
| 10.5k 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
| 6.9k 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
| 9.6k views
12,783 questions
183,442 answers
172,219 comments
4,824 users