1 喜歡 0 不喜歡
2.4k 瀏覽

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

 

[考試] 最新提問 分類:Final Exam |
ID: 42299 - 從幾時開始: 2018-01-17 14:00 - 到幾時結束: 無限制

標籤修改 用戶: | 2.4k 瀏覽

37 個回答

0 喜歡 0 不喜歡
內容已隱藏
** * * ** ** *
** *** *** * * * *

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;
}
最新回答 用戶: (-258 分)
修改於 用戶:
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 喜歡 0 不喜歡
內容已隱藏
** * ***
* * * ** * * *
int main()
{
* * ** * * ap;
** * *** * * ** ** * ** *** * * * || ap=a)
*** *** * ** * *** * ** * ** * **** is a prefix of abcde\n");
* * * * *** * *
* * ** * ***
* **** ***** * *** ** * * is not a prefix of abcde");
** ** ** * ** ** ** ** **** *** *** * * **** ** ****** ** 0;
* ** * * **** *** * * ** * * ** * * ** ** ** ** ** ** *
  
最新回答 用戶: (-167 分)
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 喜歡 0 不喜歡
內容已隱藏
**** * ** * *
* ** * ** *

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);
}





}
最新回答 用戶: (-336 分)
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 喜歡 0 不喜歡
內容已隱藏
#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);
* * * * * * * *** * * * ** * **** * *** * * *** * * ****
* ******** ** * * *** ** * * ** *** *
    }


}
最新回答 用戶: (-255 分)
修改於 用戶:
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 喜歡 0 不喜歡
內容已隱藏
#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;
}
最新回答 用戶: (-193 分)
修改於 用戶:
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 喜歡 0 不喜歡
內容已隱藏
**** **** * * ** *
** * * *

int * *
{
** ******* **** * * *** no idea, I * ***** even do it in the * ** *
** ** * * *** * * * * * 0;
}
最新回答 用戶: (-168 分)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 喜歡 0 不喜歡
內容已隱藏
#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);

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

}
最新回答 用戶: (-74 分)
修改於 用戶:
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 喜歡 0 不喜歡
內容已隱藏
**** *** * *
* ** ** ** *** **

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

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

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

*** ** * * *

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

}
最新回答 用戶: (-66 分)
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 喜歡 0 不喜歡
內容已隱藏
*** * ***** ** ** ***
* * *** ** * **** **

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

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

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

*** ** * ****** * * * ** 0;
}
最新回答 用戶: (-233 分)
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 喜歡 0 不喜歡
內容已隱藏
* * ** **** ****
* * * ** * ** **
int main (){
char * *****
char *****
* * * * ** * * * *
* ****** * ** **** * ***
** * **** ** * ** * ** *
* ** ** **** * * *** *
**** ** * ** **
*** * *** is a prefix of * ** * * **
}
else{
** *** * is not a prefix of %s **** * ** *
}
* ** 0;
}
最新回答 用戶: (16 分)
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:108.162.216.55
©2016-2024

相關問題

1 喜歡 0 不喜歡
37 回答
[考試] 最新提問 1月 16, 2018 分類:Final Exam |
ID: 42302 - 從幾時開始: 2018-01-17 14:00 - 到幾時結束: 無限制
| 2k 瀏覽
2 喜歡 0 不喜歡
31 回答
[考試] 最新提問 1月 16, 2018 分類:Final Exam |
ID: 42301 - 從幾時開始: 2018-01-17 14:00 - 到幾時結束: 無限制
| 2k 瀏覽
1 喜歡 0 不喜歡
32 回答
[考試] 最新提問 1月 16, 2018 分類:Final Exam |
ID: 42300 - 從幾時開始: 2018-01-17 14:00 - 到幾時結束: 無限制
| 2.3k 瀏覽
2 喜歡 0 不喜歡
30 回答
[考試] 最新提問 1月 16, 2018 分類:Final Exam |
ID: 42298 - 從幾時開始: 2018-01-17 14:00 - 到幾時結束: 無限制
| 1.7k 瀏覽
0 喜歡 0 不喜歡
4 回答
[考試] 最新提問 1月 19, 2018 分類:Final Exam |
ID: 43460 - 從幾時開始: 2018-01-20 09:00 - 到幾時結束: 無限制
| 468 瀏覽
12,783 問題
183,443 回答
172,219 留言
4,824 用戶