0 like 0 dislike
15.5k views

Each string will have two strings (string A and string B), each sub-string has 5 characters. 

Write a program with a function compare(string A, string B) for comparison

輸入兩個字串(string A, string B), 每個字串有五個字元

寫一個compare(string A, string B)函數。不能用字串函式庫

If String A == String B returns 0

If string A> string B returns 1

If string A <string B returns -1

Do not use string libraries

Example input:

gamesbegin

Example output: (because g>b => 1)

1

Example input:

iamigiates

Example output: (because m<t => -1)

-1

 Example input:

111qq111qq

Example output:

0

Remember: You may correct the cases, but your code always be revised! 

[Exercise] Coding (C) - asked in Chapter 13: Strings by (5.2k points)
ID: 40713 - Available when: 2018-01-04 18:00 - Due to: Unlimited

edited by | 15.5k views

53 Answers

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

int main()
{
* ** * * **** * * * ** word[10];
** *** ** ** * ****** **** * * * word);
    int i;
* ** * ** ** * = 0; i<=4; ++i)
    {
* * * ** ** * ***** * ** * * **** * * ** * *** ***** ***
* * * * * * ** * *** *** *** **
* ** ** * **** *** *** ** *********** ** ** ** * * ** * *** *** * ** * * * *** *
*** **** ** * * * ** * * * * *** * ** * ** ** * * * ** ***** *** 0;
* ***** * * * * * *** * * * * ** * **
* *** * * * *** * * * * **** * * **** * * if(word[i]>word[i+5])
* ** * * * **** ***** * ** *****
**** * ** * *** ** * ***** * * * * * * ******** * ** * * * **** ** * ** *
* * * * * ** *** ***** * * * * ** ***** ** **** * * ** ** *** * 0;
* **** **** * * * ** ******* *** ****** * * **
    }
* **** **** * ** ** * ** *** ** ****
* *** ***** ** ** ** * 0;
}
answered by (-258 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
*** * * ***** ** ***
int * *
{
* * *** * *** char word[10];
**** *** ** * * ******* * *** * ** *
** * *** *
** *** * * int i;
* *** * ** * ******
** * ** * {
** * *** ** **** * ** *** * * * *** **** **
* * ** **** * * * * *** {
* ** * * * ** *   ****   * * * ** ** **
*** * *       * * * ** return 0;
*** * * *** ** * * * ** }
* *** * * * * * ** *** ** *** ** **
**** ** * * * *** * ** ** {
** * *** *** ** * * * * * ** **** * * ******* * **
* * *** ** * * ** * *   return 0;
* * * * *** ** **** ** }
* *** *** ** }
* ** * * * * * ** ** ** *
** * * * return 0;
}
answered by (-249 points)
edited by
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Wrong output
0 0
prog.c: In function 'main':
prog.c:13:3: error: 'else' without a previous 'if'
   else if(word[i]>word[i+5])
   ^~~~
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 0
prog.c: In function 'main':
prog.c:16:3: error: expected ';' before '{' token
   {
   ^
0 like 0 dislike
Hidden content!
#include * * ** **
#include *** * ** *
#include <ctype.h>
int compare(char a[],char b[])
{
** * ** * * **** * ** ** i;
* * ** * * ** **** *** = 0 ; i < 5 ; i++)
** * * * ** **** *
** ** *** ** **** * * ***** * *** *** * **** > b[i])
** ** *** * * ** * * *** ** ** ** * ** ** * * * ** * * * ** 1;
* * * *** ***** *** * ** * ** ** * * *** < b[i])
** * *** * ***** **** ** * * * * ***** * * * ** * *** * * * -1;
**** *** * **
* ** ** ** ********* ** *** * * 0;
}
int main(void)
{
** ******* * * a[5],b[5];
****** ** * i;
* * ** * * ** * = 0 ; i < 5 ; i++)
**** ** **** * * * * *** * * *** * * * * * * ** **** ** * *
* ** ** * *** * = 0 ; i < 5 ; i++)
* * * *** * ***** * ** *** ** * * *** ** * *** ** *** ******** * **

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

*** *** * * * 0;
}
answered by (-127 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include * *** *
#include * * * * * *
#include <ctype.h>
int compare(char a[],char b[])
{
* ** * * * *** ** * i;
*** ** * * * * * * = 0 ; i < 5 ; i++)
**** ** **** * ** ** * ** *
** * * *** * ** * * *** *** * ** ** > b[i])
* * ** ***** **** *** ** * ** ** * *** * ** *** ** * * * 1;
* ** * *** ** * * **** ******* ** ** * * < b[i])
* * *** * * *** ****** *** ** *** * * ** ***** * ** * ** ***** ** * * ***** -1;
******* * *** *** *
* * ** ** **** * *** **** 0;
}
int main(void)
{
* ****** * * *** a[5],b[5];
* *** *** * * **** i;
** *** *** * * * = 0 ; i < 5 ; i++)
** * * * * ** *** ** * * ** * * ** * **** ** * ** * ** *
* *** * ** ***** = 0 ; i < 5 ; i++)
***** ** ** * * **** ** ** * ** *** * * ***** ** *

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

****** *** * ** * * ** 0;
}
answered by (-255 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
#define MAX_SIZE 5

int compare(char * str1, char * str2);

int main()
{
** ** * * * * * ** **** * * * str1[MAX_SIZE], str2[MAX_SIZE];
** ** * ***** * *** ** * res;

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

** ** * ** *** * **** *** = compare(str1, str2);

** ** * ** * * ** *** ** * == 0)
* *** * ** * ***** * *
***** ** * ** *** ** *** *** ** * * *** * ** * * ** *** * * *
**** * * ** **** * * **
*** ***** * ***** if(res < 0)
* * **** ** ** *
***** * * *** * ** *********** * * * ** **** ** *
* *** * ** * * ** **
******* *** * * ** ** * *
* ** * * * *** *
** ** * ***** ** * *** **** * * ***** * * ** * ** * * ** * ** ** ***
***** ** * ** * * * **

* * **** **** * * 0;
}
int compare(char * str1, char * str2)
{
* **** * ** * * * * i = 0;

* **** * * * * ** ** * == str2[i])
* *** * ** ** *
* * ** * * * ** * * * *** ** ** ******* ** *** == '\0' && str2[i] == '\0')
*** * * *** **** ** * * ** *** * * * ** ** * ***** * * ** * * *

** ******* * * *** ** ** ** ** ***** *******
** ** ****** * *
* * ** *** * * * * * * str1[i] - str2[i];
}
answered by (-168 points)
0 0
prog.c: In function 'main':
prog.c:11:5: warning: 'gets' is deprecated [-Wdeprecated-declarations]
     gets(str1);
     ^~~~
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:12:5: warning: 'gets' is deprecated [-Wdeprecated-declarations]
     gets(str2);
     ^~~~
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/ccwjgCDa.o: In function `main':
prog.c:(.text+0x1f): warning: the `gets' function is dangerous and should not be used.
0 like 0 dislike
Hidden content!
#include<stdio.h>

int compare(char str1[],char str2[]);

int main()
{
* * ** ** * ** * * *** * i,j;
** * * ***** * * ** * str1[5],str2[5];
**** * ** ** * ** * * *
* * *** ********* *** *** ***
** ** ** *** * *** *
* * * ** *** ** * ** * ****** *** * *** **** ** * * * * **** ***
* * * *** * * ** * **** * ** * ** * ******
** * *** * * ** ***
** **** ***** * * * ****** ***** * *
* * ** * * * * *** * *
* *** * * ** ** * ** * *** * ** * * * * * ** *** *** * * *****
** * * * ***** **
* ** ** ** * **
***** *** *** ** ****** * * * * * ** ** ** *** *
}


int compare(char str1[5],char str2[5])
{
** *** **** * * * ** ** * i,sum1=0,sum2=0;
    
***** ** * ** *** * * ** **** **** *** *
* * **** * *** * * ** ****
** * ** *** ** * **** *** * ** ** * = sum1+str1[i];
** * ***** * ***** * * * ***** *** **
** ** *** * * * *** ** ** ** ** **** = sum2+str2[i];
* ** **** * * * ** * ** * *** ** *
*** *** ** ** ** *** ** * * *** ** * > sum2) return 1;
* * *** ******* **** * *** ** *** *** *** ***
* ***** * ****** ** * * * * **** ** * * * * ** * if (sum1 < sum2) return -1;
**** * ***** **
    
********** ** * * 0;
}
answered by (323 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>

int compare(char str1[5],char str2[5]);


int main()
{
    
    int i,l;
** ** * * * * * ** str1[5],str2[5];
   
    
* * * ** ** * ** ***** * *** ***** *
    {
***** * *** * ** * * ** * *** * * * ** * * * * * ** ****
* * * ** * ** * ** * * * * *
    }
    
***** ** * ** * * * * * ** * **
    {
**** * * ******* * * *** ***** * **** ******* *** * *** * *** * ***** ** *
****** * ****** ** * * * **** ** * **
    }
    
** * ** * ** ** * * * ****
    
* * * * * * ***** *** * * 0;
}
    
    
int compare(char str1[5],char str2[5])
{
    int j;
    int k,m;
    
* * ** **** *** ** **
    
* * * * * *** * ** ** * * ** * ** * * ** *
* * ** ** * * *** ** ** * * * ** * * *
* ** * *** * * *** * * *** * * * ** * * * * * * * * *** *** * == str2[j])
* * * ***** * * * * * * * * *** * ** *** * **** ** * ** * * **** ** *
**** * ** * * * **** * ** *** **** * *** ** ** * * *** * ** ****** ** * * * ** * * ** *
**** *** **** ** ** * ***** * * * ** * ** *** ***** *
**** ** ** ** * *** * * * * ***** * **** ***** **** ** * * ****** *
** ** * * ** * *** * * ** *** * ** ******* *** ** * * * * ** ** ****
* * * * * * * ** **** ** *** ** * * * * ** ** * * * *** * ** * ***** * ** *
* ** * ** * * * ** ** ** ** * * * *** * ** * * * ** * * * * **
* **** *** * *** ******** **** ** * **** * ** *** **** ** * * *
** ** ****** ** ***** ** * ** * **** **** *** ** * **** * ** * ** **
* ** *** * *** *** * ** ********** * *** ***** * **** ** * * * *** * *
** **** * * *** * * * * ** ***** * ** * * * * * ** * ** *
** * ** *** * * * * ** * * * ** * * *
    
* * *** * * * * * ** ** ** * ** *
***** ******** ** *** * * **** *** *** *** *** * * ***
* * ** * * * ** ** * ** * **** *** *
* * * ** * * * *** * * * *** * *** * * * * ** **** * * *** * * **** **
* ****** **** ** * * * * * ** * **** * ** * * * * * * * ** * *** * ****
** * ** ** *** * ** *** *** ***** * * * **
** * ** ** * * ** *** **** * ** **
* *** ****** * *** * ** * * * *** **** ** * if (str1[m]>str2[m])
* ** ***** *** * ** * **** * *** ** *****
* ** **** * ** * * ****** **** ** * * *** ** * * * ** * ** **
* * * *** * * * ** * *** ** *** ******* **** ******** **** * * *** ** ***
**** **** *** ** *** ** * ***** ** * * * **
* ** ******* * * ** **** * * * * ** *** *** *** **
*** * * ** ***** ** ** ** ** ** ** * ***** ** * ** *** ** ****** * ***
* *** ** * * **** * * * * * ** * * * ***
* *** *** * ** * * 0;
}
answered by (-193 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
int main()
{
**** * * *** * * * ** * A[6],B[6];

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

** *** ** ** * *** * ** * *
* ** ** * *
** ** ** ***** * ** *** * *** * * * * * * ** * * * *
    }
**** *** * * * if(strcmp(A,B)<0)
** *** ** * * ** *** *
* ** *** * ** ** **** *** * * * ** * * * * *** ** * *
* ** * * *** * ***** * *
* * ** *** **** * * ***** if(strcmp(A,B)==0)
****** * **** * * *
*** ******** * * ** *** * * *** ** * * * * * ***** ** *
* **** * * ** ** **** *
* ** ** *** ** * * * *** 0;
}
answered by (-323 points)
edited by
0 0
prog.c: In function 'main':
prog.c:9:8: warning: implicit declaration of function 'strcmp' [-Wimplicit-function-declaration]
     if(strcmp(A,B)>0)
        ^~~~~~
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
char comparing (char sA[], char sB[])
{
* **** * ** * * * * * a;
//    if (sA[0]-sB[0]==0){
* * * * ** * * ** * * * * ****** ** ** *** * * * * (a=0;sA[a]-sB[a]!=0;a++){
** **** * ** * ** * * * ** * * ***** ** ** ** ****** ** * ** * * * * (sA[a]-sB[a]>0){
**** ** *** ** **** * * * * ** * **** **** ***** **** *** * ** * * * * * * * **** *** * * ** * 1;
* ** ** **** *** * ** * ** ** *** ** * ** ****** ** *** *** * ** **** * * *** *** *
** * * * * * *** * ** * * * * ** * ** * * * ** ** * **** * *
* * * * * *** * * * *** * * * *** *** * * **** ** **** *** ** * ******** (sA[a]-sB[a]<0){
** * *** * * * ** * ** * * *** * *** * ***** * ** ** *** *** ****** * ** * **** * * ** -1;
** *** ** * * *** * ** ** ***** ** * * * * ** *** * *** * * * ** * * ** ** * * * * **
** *** * * ** * ** * ** ****** ** * * * ** ** *** * * **** * **** ***** *
* * **** * ** * * ********** * ***** * *** ***
* ** **** * *** * * * * * *** ** ** ** * *** ** * * * * * *** ** ** * * * ** * *** * ** * *
******** *** ** ***** *** ***** *** * * ** *** **** * *** ** * *** 0;
* ***** ***** * * ** ** * * * * *** ***

//    }
//    if (sA[0]-sB[0]>0){
// ** ****** ** * * * *** * *** ** ** * 1;
//    }
//    if (sA[0]-sB[0]<0){
// ** *** * * * ** * * * ** *** -1;
//    }

}

int main (void)
{
* ** * * *** *** * ** b,c;
* * ** * * ** * *** * stringA[5],stringB[5];
** * * * ** ** ** ** * (b=0;b<5;b++){
** ** * *** ** ** * * * ** * * * *** ** ** *** *** * * ** *** ** *** * * ** * * ***
* ** ** * ** *** ****
* ** * * * *** * *** (c=0;c<5;c++){
* **** *** * ***** * **** ** * * * ** ** ** * ** ** * * **
    }
// * * * ** *** * * ***** *** * ** ** ** * * ** * * **
** * ** * * * **** ** ******** * *** ** ** * ** * * ** * *
}
answered by (-204 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Wrong output
Case 4: Wrong output
0 like 0 dislike
Hidden content!
* * ** * ** *****

int compare(char a[], char b[]);
int main(void)
{
  char a[10],b[5];

  int i,j;

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

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

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

}
int compare(char a[], char b[])
{
* **** * * ** ***** * * ** i;
******* ***** ** *** * ** ** **
* ** * * * ***** ***
** ** * * **** * * * * * *********** **** * * * ** *** **
* * ***** ** ** * ** * ** * * ** * ** **** * ** ** * * 1;
***** * * ** * ** *** ** * * * * ** * if(a[i]<b[i])
* ** * * * **** ** ** **** ** ***** ****** *** * * ** * ** ***** *** -1;


*** * ** ** **
* ** ** * * * ** * ** * * * * *** * 0;

}
answered by (-304 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:162.159.115.10
©2016-2026

Related questions

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
| 18.6k views
0 like 0 dislike
27 answers
[Exercise] Coding (C) - asked Jan 11, 2018 in Chapter 13: Strings by thopd (12.1k points)
ID: 41439 - Available when: Unlimited - Due to: Unlimited
| 14.4k views
0 like 0 dislike
44 answers
[Exercise] Coding (C) - asked Jan 4, 2018 in Chapter 13: Strings by semicolon (5.2k points)
ID: 40714 - Available when: 2018-01-04 18:00 - Due to: Unlimited
| 13.3k views
12,783 questions
183,442 answers
172,219 comments
4,824 users