1 like 0 dislike
11.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

 

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

retagged by | 11.2k views

37 Answers

0 like 0 dislike
Hidden content!
* ******
* *** * * ***

int main()
{
*** ** * * char *** **
*** * * * int i,c=0;
* * * * *** *   * * *** **
** * **   ** ** *** ** * * **** * **** ****
* * ** **** * * * ***** * *
* **** {
* ** **** * * *** *** *
** * ** * * ** {
** *** * ** ** * ** * ** * * * *** ** * ** * **
* * * *** * * ** **** * * * * * * * * {
* ** ** **               ** * **** * c=0;
*** *** ** **** * * *** ** *** ** * * * * ** }
* * * ** ** * * **** * * **** else **** ** * **** *** **
* * * ** * *** ** ***** * * * *** **** {
* * *** ** * * ** * * * ** ******** * ** * * * * ** ** c++;
** * *** * ** * ** * * * * ** **** * }
*** * * ** * * ** }
* ** **** *
*** ** * if(c==0)
** ** ** * * ** * ** * * * * is not a prefix of ** * **
* * * * else
** *** **** * ** *** *** * ** is a prefix of * ** ** *
*** ** ** * }
*** * * ** return 0;
}
answered by (-258 points)
edited by
0 0
prog.c: In function 'main':
prog.c:8:11: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
   scanf("%s",&a);
           ^
prog.c:9:11: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
   scanf("%s",&b);
           ^
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
0 0
prog.c: In function 'main':
prog.c:10:6: warning: implicit declaration of function 'stlen' [-Wimplicit-function-declaration]
   if(stlen(b)>strlen(a))
      ^~~~~
/tmp/ccnE3ufV.o: In function `main':
prog.c:(.text+0x5b): undefined reference to `stlen'
collect2: error: ld returned 1 exit status
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct output
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
0 0
Case 0: Correct output
Case 1: Wrong output
Case 2: Correct 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!
** * * ** * *** ***
** ** ** * * **
int main()
{
* ** ** ** ** ap;
** * * ***** ** **** * * * || ap=a)
* * * **** * * * * ** ** * * * is a prefix of abcde\n");
** * **** **
* ** *
*** ** ** * * * ** * * **** * is not a prefix of abcde");
** * * *** * ** * * * * ** **** * ******* * * * * *** * * * *** * *** 0;
** ****** **** *** ** * ******** * *** ** ** * ***** *** ** ** **
  
answered by (-167 points)
0 0
prog.c: In function 'main':
prog.c:6:10: error: 'a' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
          ^
prog.c:6:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: 'z' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
                 ^
prog.c:10:12: warning: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^
prog.c:10:12: error: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:11:12: error: expected expression before 'return'
            return 0;
            ^~~~~~
prog.c:12:12: error: expected ';' before '}' token
            }
            ^
prog.c:12:12: error: expected declaration or statement at end of input
0 0
prog.c: In function 'main':
prog.c:6:10: error: 'a' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
          ^
prog.c:6:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: 'z' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
                 ^
prog.c:10:12: warning: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^
prog.c:10:12: error: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:11:12: error: expected expression before 'return'
            return 0;
            ^~~~~~
prog.c:12:12: error: expected ';' before '}' token
            }
            ^
prog.c:12:12: error: expected declaration or statement at end of input
0 0
prog.c: In function 'main':
prog.c:6:10: error: 'a' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
          ^
prog.c:6:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: 'z' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
                 ^
prog.c:10:12: warning: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^
prog.c:10:12: error: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:11:12: error: expected expression before 'return'
            return 0;
            ^~~~~~
prog.c:12:12: error: expected ';' before '}' token
            }
            ^
prog.c:12:12: error: expected declaration or statement at end of input
0 0
prog.c: In function 'main':
prog.c:6:10: error: 'a' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
          ^
prog.c:6:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: 'z' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
                 ^
prog.c:10:12: warning: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^
prog.c:10:12: error: missing terminating " character
     printf("bc is not a prefix of abcde);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.c:11:12: error: expected expression before 'return'
            return 0;
            ^~~~~~
prog.c:12:12: error: expected ';' before '}' token
            }
            ^
prog.c:12:12: error: expected declaration or statement at end of input
0 0
prog.c: In function 'main':
prog.c:6:10: error: 'a' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
          ^
prog.c:6:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: 'z' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
                 ^
prog.c:12:12: error: expected declaration or statement at end of input
            }
            ^
0 0
prog.c: In function 'main':
prog.c:6:10: error: 'a' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
          ^
prog.c:6:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: 'z' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
                 ^
prog.c:12:12: error: expected declaration or statement at end of input
            }
            ^
0 0
prog.c: In function 'main':
prog.c:6:10: error: 'a' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
          ^
prog.c:6:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:6:17: error: 'z' undeclared (first use in this function)
   if(ap>=a&&ap<=z || ap=a)
                 ^
prog.c:12:12: error: expected declaration or statement at end of input
            }
            ^
0 like 0 dislike
Hidden content!
**** ** **** *
* **** * ** **

char p[1000],s[1000];
int i,pl,sl;

int test(char p[],char s[],int pl, int sl)
{
***** * **** *** ******* ** *** i<pl; i++)
* * ** * **
* ** ******** *** * ******* * * * * * ***** * (p[i]==s[i])
* ** ** * ****** * * *** *** * ******
* * * * ** *** *** ** ** * * * * ** * **** **** **** **** ***** ****
******** * *** * ** * ******** **** ** ** ** * ** **** * ** *** **** **** **
* * ********* *** ** * * * * ****** *** ** **** *** * ** **** ** * * *** ** ** *** ** 1;
* * *** * * ** ** * ***** * ** * **** * ****** ** * * ** ** ***
*** **** **** **** * * * * *** ****
** ** ** ** ** * * * * ** * * ** * * * if(p[i]!=s[i])
**** * ** * ** *** ** *** * * * * **
* ** ** * *** ** * ******* * * * *** * * * * ** * *** ** * ** *** 0;
*** ** *** *** * * * *** * * *** *** *****
****** ** * * **** * *
}


int main()
{
*
* * * ****
*** ** ** ",p);
* * * **** *** *****
*** * ***** ** * ***

sl=strlen(s);
pl=strlen(p);


** * ** *** && pl<sl)
{

*** ** ** * ** * ** * * * ****** is a prefix of %s",p,s);
}else
{
printf("%s is not a prefix of %s",p,s);
}





}
answered by (-336 points)
0 0
Case 0: Correct output
Case 1: Wrong 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[99],b[99];
    int l,i;
*** * * ** ** * **** * ** *** *******
*** ** *** ***** * **** * ***
* * * * **** * * *
** * * **** * ** ** ** *** * **
    {
** ** * * ** **** * *** * * * * * ****** ** *
* **** ** **** * * * ** * * * * **** **
** * **** * **** ** *** * **** * * * * ** * ***** ***** * * a prefix of %s",a,b);
* ** * * * * ** ****** ** * * * *** * * *** *** * *** * * ** ****
******* * * * ** ***** ******* ** *** *
* ** ****** * * ** * ** * * * *** *** * ** ***
** ** * **** ** * ** ** * * ** * ** * *
* * * *** *** ** * *** * * *** *** * * * * * * * *** * * not a prefix of %s",a,b);
**** **** * * * *** ** *** * * ** * * *** ** ********* ******* * **
* **** ** ** ** * *** **** * * ** * *
    }


}
answered by (-255 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: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int prefix(char a[100],char b[100]);


int prefix(char a[100],char b[100])
{
*** ** * ** ** *** * * * ***** i;
* * * ** *** * ** *** * count=0;
** * * * * **** **** *** len;

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

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


* ** * * ***** *****
** * * * * * *** ******
* *** * ** *** ***** **** * * ** ****** *** 1;
** * * * ** *** * * *
* * * *** **** *** * * *

   return 0;
}


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


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

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

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

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


** * * * *** * * * * 0;
}
answered by (-193 points)
edited by
0 0
prog.c: In function 'main':
prog.c:45:15: warning: format '%[^
   ' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%[^\n]*c%",&a);
               ^
prog.c:45:20: warning: spurious trailing '%' in format [-Wformat=]
     scanf("%[^\n]*c%",&a);
                    ^
prog.c:46:15: warning: format '%[^
   ' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
     scanf("%[^\n]*c%",&b);
               ^
prog.c:46:20: warning: spurious trailing '%' in format [-Wformat=]
     scanf("%[^\n]*c%",&b);
                    ^
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 like 0 dislike
Hidden content!
* ** * **
* * * ** *

int * ***
{
* ** *** * * ******** * ******* ** no idea, I * even do it in the **** * * *
* * * ** * *** 0;
}
answered by (-168 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include **** * * * **
#include * * ** ** ** *
#include * * * *

int pref(char[],char[]);

int main(){

** ** * * **** * * ** p[10],s[10];
** ** ** * * **** **** * num;

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

* * *** *** ** *** = pref(p,s);

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


 return 0;
}

int pref(char p[30],char s[30]){
**** ** * ** **** ** i;

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

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

}
answered by (-74 points)
edited by
0 0
prog.c: In function 'main':
prog.c:12: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:13: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__;
              ^~~~
/tmp/cc0tBG5r.o: In function `main':
prog.c:(.text+0x1f): warning: the `gets' function is dangerous and should not be used.
0 0
prog.c: In function 'main':
prog.c:10:13: error: stray '#' in program
     int num;#include <stdio.h>
             ^
prog.c:10:14: error: 'include' undeclared (first use in this function)
     int num;#include <stdio.h>
              ^~~~~~~
prog.c:10:14: note: each undeclared identifier is reported only once for each function it appears in
prog.c:10:23: error: 'stdio' undeclared (first use in this function)
     int num;#include <stdio.h>
                       ^~~~~
prog.c:14:1: error: expected expression before 'int'
 int pref(char[],char[]);
 ^~~
prog.c: In function 'main':
prog.c:23:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
     scanf("%s",&p);
             ^
prog.c:24:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
     scanf("%s",&s);
             ^
prog.c: In function 'main':
prog.c:51: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:52: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__;
              ^~~~
0 0
prog.c: In function 'main':
prog.c:14:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
     scanf("%s",&p);
             ^
prog.c:15:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[10]' [-Wformat=]
     scanf("%s",&s);
             ^
0 like 0 dislike
Hidden content!
* * ** *****
* * ** * *** **

int main()
{
** * length1, ** c;
* * ** * a[100];
** * ** b[100];
** * ** d[100];

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

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

** * * ** **** *

* * ** ***** *
**
* ** * * * * * ** * is a prefix of **** *
** * * *
** ** *** ** *
** * **
* * *** *** * is not a prefix of * ** ** *
** * ***

}
answered by (-66 points)
0 0
prog.c: In function 'main':
prog.c:11:10: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
  scanf("%s",&a);
          ^
prog.c:12:10: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
  scanf("%s",&b);
          ^
prog.c:24:11: warning: format '%s' expects a matching 'char *' argument [-Wformat=]
  printf("%s is not a prefix of %s,a,b");
           ^
prog.c:24:33: warning: format '%s' expects a matching 'char *' argument [-Wformat=]
  printf("%s is not a prefix of %s,a,b");
                                 ^
0 like 0 dislike
Hidden content!
* * ** ** * * **
* * * * ** * *

int main()
{
**** * * * ** ** * * ** str;
* ** ******* * *** *** * * i, p, s;

* ** * * * * ****** ** **** *** *** * * &p, &s);

** * * * * * (i<=0; &p; i++);
** * ** *** ** * * * *
* * ** *** * * **** ** * * * * * * * &p);
* * ** ** * * *** ***** ** * ** * * *** *
* **** ***** ****** * **
****** ** * * *

* ** ** * **** *** * * 0;
}
answered by (-233 points)
0 0
prog.c: In function 'main':
prog.c:12:7: error: expected '(' before '{' token
     if{}
       ^
prog.c:13:12: warning: too many arguments for format [-Wformat-extra-args]
     printf("abc", &p);
            ^~~~~
prog.c:14:13: warning: format '%d' expects a matching 'int *' argument [-Wformat=]
     scanf("%d");
             ^
0 like 0 dislike
Hidden content!
***** ******* ***
*** * * * ** * *
int main (){
char **** *
char * ** **
** ** * ** ***
*** ** * ** ** * * ******
*** ** * ** * * **
* * * * ** ***** ********
** * * *** ***
* ** * * is a prefix of ***** *** *** **
}
else{
* ** **** * is not a prefix of %s ** * * ** * **
}
*** 0;
}
answered by (16 points)
0 0
prog.c: In function 'main':
prog.c:7:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
 scanf("%s",&string1);
         ^
prog.c:9:9: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
 scanf("%s",&string2);
         ^
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.71.254.239
©2016-2025

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
| 11.2k 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
| 10.2k 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
| 11.7k 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
| 9.2k 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
| 2.3k views
12,783 questions
183,442 answers
172,219 comments
4,824 users