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

27 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
char prefix(char string1[],char string2[])
{
    int i,s;
*** * * * * * * *
* ***** ** *** * * * * * * *** * * *** **
    {
* * **** ** * * **** * * ** * *** (i=0; i<=strlen(string2)-1;i++)
** ***** *** * * *** * **** ** * *** ** * *
* ** * * ** * *** * * ***** * ** *** ** * * * ** * * ** ** * ******* ** (string2[i]==string1[i])
** *** * * * * * ** * * ** **** ** ** * * * ** * * ** * ***
****** * ** ***** * * **** *** ** * * ** ***** ** *** * *** * * * ** *** * * * *
* ** ** * * ** * * * * * * *** ** * * ** ***** ** ** *** * * ***** *
* * * *** ** ** *** **** ** ** *** *
** **** ****** **** *** * ** * * ** **
* ***** ***** ** ** * *** * * ***
* * ** ** *** ** * * * ***** *** *** ***** ** *** *** * * * * ** ** ** * ***** is a prefix of %s", string2, string1);
*** * * * **** * *** * ** * ** * * * *
** * * ** * **** * *** * * *** ** ** * * ** ****
* * *** ** * * * * *** ** * **** * * * **** **
* ** * ** **** * *** * * *** * *** * ** * * *** *** ** * * * is not a prefix of %s", string2, string1);
*** *** ** * * ****** * * * * ** ** * * *
    }
* *** * **** * * ** ** * * * * * *** * ***
    {
*** * * **** * *** * ** *** ** * ******** * (i=0; i<=strlen(string1)-1;i++)
** ** * ******* *** * * * ** * * ** * * ** * * **
** **** *** ***** * ** * **** * * * * ** * ** * * * *** * * * ***** (string2[i]==string1[i])
**** * **** * ** * ********** * ** *** ***** ** ** * ** * ***** ****
*** *** * * * * ** **** * * *** * * ** * * ** * ** ** ** *** ** ** **** * * * **** *
* *** ************ * *** * ******* ******* ** * ***** * * * * **
** *** * * *** *** ** * ** *** * * * * * * *** *
* * * * *** *** *** ** ** * *** * **
***** * * * * * * ** * ** ** *** * *** * *
** ** * * * ** ** ***** ***** * *** * * * * ** ** ** *** ** *** ** * * * ** is a prefix of %s", string1, string2);
** * *** * * ***** * * * ** ** **** * *** *
** *** * * * * ** * * * *** ** * ** *
* * ** * *** * * * ** **** **** * ** ** * ** **** *
* ***** * ** ****** *** *** *** ** * ** ** * * *** *** ** * ** ** * * is not a prefix of %s", string1, string2);
** * * ** * ** ** * * ** * * ** * **** ** * *
    }
}
int main (void)
{
    char chaine1[1001], chaine2[1001];
** * * * ** ** * * * * *** %s", chaine1,chaine2);
* * * ** *** * * ** * * * *
** **** * **** ** ** 0;
}
answered by (-168 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: 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!
#include <stdio.h>

#include <string.h>

int main()
{
* **** * ** * lenght1,lenght2,c;
** * ** *** ** ** *** b[100];
* ** ** * *** ******* a[100];
* * ** ** * ******* * d[100];
** * * * ** * * ** * * ** * *** ** *** **
* ** * ********* ** * * * * ***** ***
* * ** *** * * * * * *** **
* * * * * ** *** * * *** *
* *** * ** *** * * * *** *
** * ** ** * * * * * **
**** * ***** ** ** ** ** ** ** * *** * * is a prefix of %s",a,b);
* * ** * ** * ** * * **
* ** * ******** ** *** **
* *** * * *****
** * ** *** * *** * **** ******* * * * * * * **** * is not a prefix of %s",a,b);
** ***** ** * * *** * ** *
*** * **** * *** ** * *** * 0;
}
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 * * * ** *
#include * ********* *
//#include <ctype.h>
#define n 100
int aa(char A[],char B[])
{
** * * * i;
* ** *** ** ***** ** * *
  {
** * *** * * *** * * *** ** ***  A[i]!='\0' && A[i]!=B[i]   )
* * ***** * * * * ** *** * * * ****** *** * * ** **** ** * 0;
* * * * ** * * * * ***** * * **** * * ** ** *
** * * ** ** ** * * * * * ** * * ** * **** * *** ****
  }
* ** * 1;
}


int main()
{
**** * * * * a[n],b[n],c[n];
****** * ** * * *** i;
* * * * ** *** ** *** *** * * * * ** **
* * * ** * * * * **** * * **** * **
* ** ** *** * * * * (aa(a,b)){
* * * * *** ** * * * ***** * *** * ** * * * **
** * * ** ** * * * * * ** ** ** * * * * * * *** ** ******** **
** * ** ******* ** * * * **** * ****** * *** ** ******* * *** ******* * * ***
* * * ** *** ** **** *** * ** **** *** * ******* ** * * *

* * * * * * *** * **** * *** * *** *** *
* * * ** ** *********** * * ** * *** * *** ** * * * * is a prefix of %s",c,b);
* * * ** **
** * ** * * * **** * ** **
* * * * * * ******* *** ** *** * * ** * ** * is not a prefix of %s",a,b);
*** * *** * * * * * * * 0;
}
answered by (-32 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include ** * ***** *
** ** ** ** * **
int main (){
char * * * *
char * ** *
** **** * * string1: * * * *
* ** **** * * *** *
* * * ** string2: **
* * ** * * *
** * * ***** * ** **
* * * ** **** * * *** ** * * ** is * * * ** of * *** * * *** *
else
* * ** ** ** **** **** * *** * is not prefix of * * * * * * * ***

return 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);
         ^
0 like 0 dislike
Hidden content!
#include **** ** *
#include *****
#include *** * ** *
#include * * ** *** *
int prefix(char p[],char s[])
{
* * **** * * * * ** **
****** ** * ** *** * ** * 0;
*** * * ***** * ** * if * **** * *****
* *** * ** *** ** ** * ** 1;
** ** * ** *** * ***
* ** ****** * * * *** * 0;
}

int main()
{
* * ***** ** * * p[999],s[999];
** ** *** *** ** * * * *** ** **
* * * *** * ***** ** ** ** ***

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

* ** * * * * **** * *** 0;
}
answered by (-16 points)
edited by
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 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include *
int main()
{
* ** ** * ** * * * * length1,length2,c;
* * ** ** **** * * * a[100];
**** ****** * * ** * b[100];
* ** * * * ***** * * d[100];
** * *** * **** * * ***** * * * ** * * *
****** * ***** ** **** ** **** * ** * **
* * * ** * *** ***** ** * ** *
* ********* ** ** * * ** *
if (strcmp(a,d)==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 * ** * * **
#include *** * ***

int prefix (char * * * str2[])
{
* * * * * ** **** * * * length=0, * *
* ** ***** * *
* **** * * * ** * * * = ** * * * *
** ** * ** *** ***** *
*** ** ** **** ** ** * * * ** * **
* ** * ** *** *** ***** *** **
** ****** *** *** * if **** *
** * ** * * *       * **** * ** *** *  
* ********** * ** *** * * *   }
** *****         * * * ** *
* * *** ** * * (count == length)
* * ** ** ** * * ** * ** * * 1;
** * ** ** * else * * * 0; ** * * *** * **
* * **** ** ** * * *
}
int main(void)
{
* * * * * **** * * ** ** * * * **
* ******* * ** ** *
* * **** *** ** * * * *** * *** * * str1, str2);

* ** ***** ** ********** * * * *** *
* * * * {
** * *** * ********** * * *** ********** is a prefix of *** **** str1, str2);
*** ** ** *** * ***
* * * ** *** * **** **
***** * * * **** * *** **** * * ** * * * * * ** is not a prefix of ** ** str1, str2);

* * ** *** * ***** ** * * 0;
}
answered by (-304 points)
edited by
0 0
Case 0: Wrong output
Case 1: Correct 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!
#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 (-108 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 (-258 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
*** **** * * *
* *** * * * *
* * ** ***** *** *

int main()
{
** * * * * * *** char * ** *
* * * * ** char * * *
** * * ** **** * * ** * *** word);
*** ** * int i;
* ** * * * for(i = 0; **** ** * * ++i)
**** ** {
* **** *** * * ** * * ** * *** * * * *
* ** * * ** * ** * * ** * {
* * ** * * *** * * ** * * * ** ** * * is not a * of * * * ** **** word);
** * * ** * **** ** * * ** **** 0;
** ***** * * * * * * *** }
** * * ** ** }
* *** ***** * * * ** is a prefix of *** * * word);
* * ** ** 0;
}
answered by (-249 points)
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:104.23.197.64
©2016-2026

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
| 17.2k 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
| 11.4k 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
| 15.6k views
12,783 questions
183,442 answers
172,219 comments
4,824 users