1 thích 0 k thích
4.9k đã xem

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] đã hỏi trong Final Exam
ID: 42299 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn

chọn từ khóa lại bởi | 4.9k đã xem

37 Trả lời

0 thích 0 k thích
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;
}
trả lời bởi (-258 điểm)
đã sửa bởi
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 thích 0 k thích
Hidden content!
** ** **
***** * *** * *
int main()
{
******* * * * ap;
* **** * * ***** ** * * ** || ap=a)
** * * * * **** ** * * * is a prefix of abcde\n");
* ** **
*** **
** *** * * * ** *** ** * is not a prefix of abcde");
* ** * ** ** ******* ** * * * *** * *** ** ** ** * * ***** * 0;
* ** *** ***** * * ** * * * * * * *** *** ** *** * * ***
  
trả lời bởi (-167 điểm)
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 thích 0 k thích
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);
}





}
trả lời bởi (-336 điểm)
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 thích 0 k thích
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);
******* *** ** * * *** ** ** * ** * * * *** * * ******* *** ** *
** ***** * ***** * ** * * * * ** *** ********** *
    }


}
trả lời bởi (-255 điểm)
đã sửa bởi
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 thích 0 k thích
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;
}
trả lời bởi (-193 điểm)
đã sửa bởi
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 thích 0 k thích
Hidden content!
* ** * ** * * * ***
* *** ** * * **

int ***
{
** * ** * *** * ** * * * * * * * no idea, I * even do it in the * ** * * ** *
** * * * * ** * *** 0;
}
trả lời bởi (-168 điểm)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 thích 0 k thích
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);

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

}
trả lời bởi (-74 điểm)
đã sửa bởi
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 thích 0 k thích
Hidden content!
* * ** * ***
**** * * * * *

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

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

* * * *** ** *

*** * *

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

}
trả lời bởi (-66 điểm)
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 thích 0 k thích
Hidden content!
* *** *** * ****
* * ****** * ** *

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

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

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

**** * * ** * * * ** ** * 0;
}
trả lời bởi (-233 điểm)
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 thích 0 k thích
Hidden content!
*** ** * * ** **
** * *** ** * * **
int main (){
char * * **
char * ** *
** * * * * **
* * * ** ** * * * ** * **
**** * **** * ** * *
** **** * **** *** ** * **
** * *** ***** ** ***
**** * * * is a prefix of * * ** *** * **
}
else{
* * ** * * * is not a prefix of %s * * *
}
*** 0;
}
trả lời bởi (16 điểm)
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.255.84
©2016-2024

Những câu hỏi liên quan

1 thích 0 k thích
37 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42302 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 5k đã xem
2 thích 0 k thích
31 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42301 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 4.4k đã xem
1 thích 0 k thích
32 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42300 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 4.7k đã xem
2 thích 0 k thích
30 trả lời
[Exam] đã hỏi ngày 16 tháng 1 năm 2018 trong Final Exam
ID: 42298 - Xem được từ: 2018-01-17 14:00 - Hiệu lực đến: Không giới hạn
| 3.7k đã xem
0 thích 0 k thích
4 trả lời
[Exam] đã hỏi ngày 19 tháng 1 năm 2018 trong Final Exam
ID: 43460 - Xem được từ: 2018-01-20 09:00 - Hiệu lực đến: Không giới hạn
| 943 đã xem
12,783 câu hỏi
183,443 trả lời
172,219 bình luận
4,824 thành viên