1 like 0 dislike
8.6k 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 | 8.6k views

37 Answers

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


int isprefix(char prefix[], char txt[]){
* * * *** *** * * * i,flag=1;
** ** * * * **** *** *** * * * * *** * * ** * **
* *** ***** * ***** *** * ** ** * * ***** * * * * * *
**** * ** ** * * *

** * ** * *** * *** ** * flag;
}


char prefix[100],txt[100],i;

int main(){
* ** * * ******** ***** ******** * *
* *** * *** * *** ** **** *** * *** ** *

* * * * ** ** ** ** * * ***** *
* *** * ** * * ***** * * ** * ** **** * ** * * **** * **** * * *** * *** * *
* * ** * *** * * * * * * * * **** * *** * * * ** * * is a prefix of ",prefix, txt);
*** * ** ** *** * * * ** * ** ** * *** * ** * * * ** * * * ** ** ***
***** * * **
* *** ** ** * ** ***** {
* ** * ** * * * *** * ********** * **** * * * * * ** * * ** * * ** * *** *** **
* * *** **** ***** **** **** ** * **** *** * ** *** *** is not a prefix of ",prefix, txt);
******* * ****** * ** * * * ** * * **** **** ** * ***** * * ** *** * * *****
* * *** ** * * *
*** ** * * ** * * * * ** 0;
}
answered by (-286 points)
0 0
prog.c: In function 'main':
prog.c:24:16: warning: too many arguments for format [-Wformat-extra-args]
         printf(" is a prefix of ",prefix, txt);
                ^~~~~~~~~~~~~~~~~~
prog.c:29:16: warning: too many arguments for format [-Wformat-extra-args]
         printf(" is not a prefix of ",prefix, txt);
                ^~~~~~~~~~~~~~~~~~~~~~
0 like 0 dislike
Hidden content!
** * * ** ***
** ** * * * ** ** *
char prefix(char string1[],char string2[] )
{
* ** ** * * * * *** * * i,a;
* ***** * * * * * * ********
* * * ***** * * * **** * ** *****
* *** **

* *** * *** * * ******* ** * ***
  {
* *** * ** * * ** ** ** ** * *** ** * * * ******** ***
* * * * * * *** ***** * * * * **** **
* ** ** * *
* * * ** * ** *** ******* *
** * * * * **** * **** * *** is a prefix of %s", string1,string2);}
** ** ** ** * ***** ** * is not a prefix of %s", string1,string2);}
* ** *******
* **** ** ****
* * *****
**** * ** **** * * * ***** *** * * ** ** * * * * * * *** ** **
** ***** *
* * * * ** * **** * * * ***** ***
* * * * * *** * ** * *** * *
*** ********** *** *** **** * * ***** ** *** * ***
**** * * * * *** * * ****** *
* *** *** *
* * ** ** ** * * * *** * *** *
*** * * ** * * * ** * *** * **** * is a prefix of %s", string2,string1);}
** ** ***** ** **** ** *** * is not a prefix of %s", string2,string1);}
**** *** **

}
int main(void)
{
****** * ***** chaine1[1001], chaine2[1001];
*** * * ** *** **** * *** ** * *
* *** ** ** * ** ** * ***
**** * ** *** **** *****
* ** * *** 0;
}
answered by (-168 points)
0 0
prog.c: In function 'main':
prog.c:37:11: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[1001]' [-Wformat=]
   scanf("%s\n",&chaine1);
           ^
prog.c:38:11: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[1001]' [-Wformat=]
   scanf("%s",&chaine2);
           ^
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 like 0 dislike
Hidden content!
#include<stdio.h>
* **** * ** * **
******** ** ** ***
int main(void)
{
** ** **** ** ** **** * str1[100],str2[100],i;
* ** ** ** ***** * * * ** *** ** ****
* ** ** * * **** * * ** * * **** **
**** * * * **** *** *
* *** * * * ** **** ** * ***** ***** * * ** * * *
* ** * * ***** * * *** * * * * * ** * ** **** * ** * * ** * *** ** * * * is not a prefix of %s",str1,str2);
* * *** * * *** ** ** ****** *** * * * * * *** * * *** * ** ** ** * * 0;
* *** ** **** * * ** * * * ** ** ** * * ** * ** * * ***
** ****** * **** ****

 printf("%s is a prefix of %s",str1,str2);
*** * ** ** *** * * **** * * 0;
}
answered by (-304 points)
0 0
prog.c: In function 'main':
prog.c:6:13: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int *' [-Wformat=]
     scanf("%s%s",str1,str2);
             ^
prog.c:6:15: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'int *' [-Wformat=]
     scanf("%s%s",str1,str2);
               ^
prog.c:7:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     for(i=0;i<strlen(str1);i++)
               ^~~~~~
prog.c:7:15: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:7:15: note: include '<string.h>' or provide a declaration of 'strlen'
prog.c:7:22: warning: passing argument 1 of 'strlen' from incompatible pointer type [-Wincompatible-pointer-types]
     for(i=0;i<strlen(str1);i++)
                      ^~~~
prog.c:7:22: note: expected 'const char *' but argument is of type 'int *'
prog.c:10:23: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int *' [-Wformat=]
             {printf("%s is not a prefix of %s",str1,str2);
                       ^
prog.c:10:45: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'int *' [-Wformat=]
             {printf("%s is not a prefix of %s",str1,str2);
                                             ^
prog.c:15:11: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int *' [-Wformat=]
  printf("%s is a prefix of %s",str1,str2);
           ^
prog.c:15:29: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'int *' [-Wformat=]
  printf("%s is a prefix of %s",str1,str2);
                             ^
0 0
prog.c: In function 'main':
prog.c:8:15: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
     for(i=0;i<strlen(str1);i++)
               ^~~~~~
prog.c:8:15: warning: incompatible implicit declaration of built-in function 'strlen'
prog.c:8:15: note: include '<string.h>' or provide a declaration of 'strlen'
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()
{
* * * * * * ** * **** str1[100],str2[100];
** * ** ** * * * * i,c=0;
* ** *** ** * ** ***** *** * ** * * **
** * ** * ** * * ** * * ** * * * **
* ** ***** * **** * ** * **** ***
* * ** * * *** * ** ** ** * **
* ** ** *** ****** * *** ** *** ** ******** ** *
** ** * ** * *** * *** * ** * *** *** *
* ** ** ** ** ** **** *** * * * * ** ** * ** ***** * ** * *
** * * * * * * ******* *** * ***** *** * *** *
    }
* * * * ** **** *** ** **
* * * * ** * *
* * * ** ** ** ***** * ** * * ** * * **** ** ** ** * is a prefix of %s",str1,str2);
* * * * **** * *** **** *
** * *** * * *** **** *
** ** ******** * ***
***** * **** *** * * ***** * * ** * * * * * * **** * is not a prefix of %s",str1,str2);
* * * ** * ** * * *
}
answered by (-140 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(void)
 {
* * * **** ** * ** ** * * pre[100],str[100];
**** * * * * ** * * *** * ** i;
** ****** * ** * * * ** ** ** **** ** **
** * * * ** * ***** * * * * * * * ** ***
*** ** ** * * * * * *** * * ** ** * * *
    {
** * **** *** * ******* * * * *** * *** * * * * * * * *
***** * * * * ** **** * **** * *** * ****** *** ** ** ****** * *
** ****** * ***** * ** ** *** **** ** ** * *** * ** * * *** *** * *** ** ****** ******* is a prefix of %s",pre,str);
* ***** **** * * * *** ** *** ** * ** * ** * * ** * * * * **** * * * *** * **
* ** ** * * * ** * * *** **** * * ** * * * ** * * * * **
* * ** ** *** * ** *** *** * ** * ** **

* * * * * ** ** * * ****** * ** **** **** * * ** * **
** *** ** * * ** ** ** *** ** * ** * ** * ** * * * **** * * *** * * * *** * ******* *** is not a prefix of %s",pre,str);
* * ***** ** ** ** * *** ** * * **** *** * **** * * *** *** * * **** *** *** * ** * ** * *
* * ** ****** * * **** ** * ** ** * **** * * ** *
    }
    return 0;
 }
answered by (-281 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()
{
* ****** * ** ** str1[100],str2[100];
* * * * * *** * * * * i,c=0;
** * *** ** * ** ** ** *** *
*** ********* * * * **** *** * ** **
* * * * * * * * **
** * ** * * * ** * * * *
* ****** ** ** * ** *** * * * ** * * * ** *
* * * * * **** ** * * ** * ** * * ***
**** ** * * * ** * ** *** * *** *** * * ** ***** **** * * * * *
** ***** * ** *** ***** * * ** * ** * * *
    }
*** ****** ** **** * ****
**** * * ** ** * ****
* ** * * ** *** * ** * * ** * ** * * * * * ** * ** is a prefix of %s",str1,str2);
* ** * ** * * * **
** * *** * ***** ****** *
* * ***** ****** ** * ** **
* *** ** * * * *** ** ** * ** * ** ** * is not a prefix of %s",str1,str2);
* **** * * *** ***
}
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 * * ****
#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 by (-254 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()
{
*** **** ** * * **** n,count;
*** ** ** * * *** * ****** * * a[100],b[100]={0},c[100]={0};
* *** ** ***** *** ** *** *** ** %s",a,b);
* * * *** *** * ** ** **
** * ** * ** * ** * * ** * * **** **** ** * **
** * * ** ** * ** ** * * *** ** * ***
* ** * *** *** ** * *
* * ** **** * ********* ** * * ** ** ** * ** ** *
* * ** * * ** ** ** ** **
* *** * ** *** ****** ** * * *** **
*** ** * * ** (strcmp(a,c)==0)
* * * *** **** ** *
** * ******* * ** * **** * * * ** ** ** *** ** is a prefix of %s",a,b);
* ** ** * * * * * ****
* *** * * * * ***** **
** ** * ****** * ** *
** * **** * ** ** * * ***** * * **** * * *** * is not a prefix of %s",a,b);
* * * *** ** *** ** ***
** ****** * * *** * * * *** * 0;
}
answered by (-229 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()
{
** * *** ** *** ****** * String[50],string[50];
* ** * ** ** ***** ** * a,b=0;
* * *** * ** * * *** * ******* ****** * *** * **
***** * *** * ** *** *** **** **** *

** ****** *********** * ** ***** *
* ** * *** ** ** ***
* **** * * *** ** * * * * ***** ** ***** ** * ***** * * ** *
* * * * *** **** ********* ** ** * **
* *** * ** ** ** *** **** * * ** ** * * ***** ** * * ***** ** * * * *
* * * ** * * *** * * * * ** ** * ** *** **
    }
* * ** ****** * ****
*** *** ** ** * * ** *
* * *** *** ** * ** * * * **** * ** ** **** *** * * is a prefix of %s",String,string);
** ** * ********
* * * ***** ** ** *** *
** * * * * **** **
* *** ** ** * * * * ** ** * ****** * **** * ** is not a prefix of %s",String,string);
* *** * * *** * ***
  return 0;
}
answered by (-107 points)
edited by
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 like 0 dislike
Hidden content!
#include <stdio.h>
#include * * ** **
#include ** * **** ** * *

int main()
{
** **** * * * * ** * a[100],b[100];
** ** ** **** **** * * * *** * %s",a,b);
* ***** ** * * * *** * ** ** i,j,prefix = 1,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: Wrong output
Case 2: Correct output
0 0
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.71.254.148
©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
| 8.9k 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
| 7.7k 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
| 9k 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
| 7k 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
| 1.7k views
12,783 questions
183,442 answers
172,219 comments
4,824 users