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

53 Answers

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

int compare (char a[],char b[]);
int main()
{
 char a[6],b[6];
 int c;
* * * *** *** ** * ** *** ****** is equal to * * * * **** * * ** * ** * ** * *
c=compare(a,b);
** ** **** ** * *
}


int compare(char a[],char b[])
{
* *** *** *** * * ** *** x,y,z;
* *** **** * * * * * * ***** * ** *
**** *** * *** ****

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

* ** * * *** *** ****** ** ** ****** (x>y)
*** ** * * * * ****** * * ** ***** *
* * * * *** **** ** ** * *** *** * *** ** * ** ** * * 1;
* ******* * * ** ** * * *** * ** **
*** *** ***** ** ** * * * ** *** * *** if(x<y)
* **** ** **** * * * * ** ** *** ** * ***
** * *** * * ** *** ** ** * *** * * * * * * * *** * * * * * ** * *** * -1;
** * * * ****** * *** * * * * *** * ******* ** *
* ** ** **
**** ** *** ** ** ** * * 0;

}
answered by (-329 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 stringA[],char stringB[]);

int main(void)
{

** * **** ** *** * * * n1[10],n2[5];
* ********* * * * * * * i,j;

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

** *** * ** *** * ** * * j=5; i<5; i++,j++)
* * * * ** * ** * ** * *** ***** * *** = n1[j];

* ***** * *** * * ** **** ** *** ***** * (n1,n2));

}

* * * *** ***** * * compare (char stringA[],char stringB[])
*** * * ** ******** *

* *** * ** *** ***** ** ** * ** * *** * * ** * * i;
* ***** * ***** * * * **** ** ** ** * * * ** ** ** *
****** ** **** *** *** ******* * *** ***
*** ** * * * * * ** * * ** **** *** **** *** *** * *** **** *** * ** * ** * *
** ** * * ******* * ** ** ** **** *** ** ****** * * ** * * ***** ** ** * * * 1;
******* * * *** **** * * *** **** *** ***** * ** *** ** ** * *** if(stringA[i]<stringB[i])
* **** ** ** * * * ** ** *** * * ** ** * * * *** *** * * **** **** ** *** * ** ** * -1;
** ** * *** **** * *** * * ** * *** * *
** *** ** ***** ** * **** ** ****** ** ** * ** * * 0;
    }
answered by (-281 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 a[],char b[]);

int main()
{
* * ****** **** ** * a[50];
** * * ****** ** ** b[50];
*** * * *** ** *** *** z;
*** *** * ****** ** ****** ** ***** *
* * * * * * * ***** ** ** ** *
* * ** * * *** **** *** ** ** * * ** *




}

int compare(char a[],char b[])
{
*** * *** * ** * * * * * q,r,c;
*** *** ** * * * * * * * (c=0;c<5;c++)
    {
*** * * ** * ******** * * ** *** **** * *** *
** ** **** ** ** * * ** * ** * ** * * * *** ** * *
* ** **** * * * ** ** * * * **** * * **** (q>r)
** ** ** *** * * * ** * ** ***** *
** *** ** ** ** * * * ****** **** ** * *** * * 1;
* ** *** * * **** *** ****** ** * * ***
** ***** ** ** * * * **** *** *** **** if (q<r)
* ***** * ** * * * *** ** * ** ** **** *******
* ** ** * ** ** * ***** ** *** **** ** * **** **** ** * * -1;
** * * * * * ** ** ** * * ** ******* *
    }
* ** * * * **** ** *** 0;
}
answered by (-229 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 a[],char b[])
{
* **** * * *** i,j,k;
* * * * ** *** * * * ** * *
    {
* * ** *** * ** **** ******** * **** ****** ** **
* ** * * *** * ** * ** ** * ***** * ** * ****** * ** * * ***
* ** ** *** * * * * ****** * *** ****
* ** * * * * * ** * *** ** * ***** * ** * *** ***** ** **** **
* * * **** ** ** * * * * ** *** ** ** * * * ** * * * ** 1;
* *** **** ** * * ** ** * ** *** *** *
** * *** * *** * ***** * * **** * * ********* if(j<0)
** ** * ** **** * ** * * ** *** * * * **** *** **
** ***** ** * * * * * ***** * **** ******* *** * ****** * *
* ** * * * * * *** ****** * **** ******** * *** ** ** * ** ** ** *** -1;
*** *** **** ** * *** * * * ** * **
    }
*** ** ******* * * * **** ** *
    {
* *** * *** ** * * * * ** * * ******* * * * *
** ** **** * ** **** * * *** ** ** ** * * * * *
* *** * ** **** * ****** *** * * * * * *** * ***** * *** 0;
** * ** ** * ** *** *** * * ** ** * *
    }

}
int main()
{
  char a[5],b[5];
  int i,j;
* * ** ** * **** **
  {
** ** ** ** * * **** *** **** ** * ***** **** ****
  }
** * ** **** **** * * **
  {
* ** * *** * ** * * ** ***** * * * * * * *** * ** ***** * ** ****
  }
* ** * *** * * * * ** * * ***** * *
}
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>

char a[5],b[5];
int asum=0,bsum=0,i;
int compare(char a[5],char b[5])
{
    for(i=0;i<5;i++)
** * * *** **** * *** *** * ***** **** * *** *
** ** *** **** ********** ** * ** * * * *** * * * * * ** ****** *
** * * * *** ** ** * ** * * ** * * ** * *** ** * * ** ** *** *****

* * * * ** * * ** ********** *** * ******* * ** *** ** * ** return 1;
*** * *** * ***** ** * * ** **** * ** ***** * * ** * ***** * *** if (asum<bsum) return -1;
** * ***** *** *** *** ** ***** **** *
    if(asum==bsum) return 0;


}

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

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



* **** *** **** ** ** ** * * * * *** * **
}
answered by (-301 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 a[],char b[]);

int main()
{
***** ** ** * ****** ** a[50];
* * * *** ** * * *** b[50];
    int z;
* ***** ******* * *** * * ***** * ****** *
** *** ** ********* * *
** * * ** **** * **** ** ** * *** * *




}

int compare(char a[],char b[])
{
* ***** * **** * *** q,r,c;
* ***** ***** *** * ***** (c=0;c<5;c++)
    {
*** * ******** *** ** * * * ** ** * * * *** *
* * * ***** **** * *** ** *** * * * * *** ******
** ** **** * * * * * ***** ** ** (q>r)
* * **** *** * * *** ** ** * * ** ** ***
** *** ** * ** ** * * * * **** ** **** *** * * * * ** ** * ** ******* 1;
** * ** ** * *** * ** * *
* * * * **** * ** * **** *** ** ** * if (q<r)
*** ** ** *** *** * ** * *** * * * * ***
* * ** * * *** ** * * * * *** * *** **** * * *** * ** * -1;
****** * * *** * * **** **** ** * ****** *
*** ** * ** * * **** *** *** ** * * **** *
* ** ** * * * * * * ** * * * * * * * *** * *** * * ** 0;
    }

}
answered by (-229 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!
#include <stdio.h>

int compare(char *a){
* * * ** * ***** ** ******* ** b;
** * * * *** ** ** * **
***** * **** ** * *** **** * ** **** * * *** * **** * *
* * ** * ** * * * * * * *** *** ** ** *** ****** * * ** ** ** 1;
* * ** ** * *** * * * * ** * ** **** if(a[b]<a[b+5])
* ****** * ** *** ** ** * *** ** ** ******** * ** * ** * * * * * ** -1;
    }
**** ** ** * * * ** *** 0;
}

int main(){
**** ** *** * * * a[10];
** * * * ** **** * * **** * * *** ** * *
*** * * * *** * *** ** ** ** ** * * **
** ** * **** ** ****** * 0;
}
answered by (-498 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: 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 S[10],A[5],B[5],i;


int compare(char A[5], char B[5])
{
* ** * ***** ******** a_sum=0,b_sum=0,i;
 // **** * * * * **** ***** **** * ** * ****
  // ** ** * * *** * * * *
** * ** * * *** ** * *** i<5; i++)
** * ** * * * * **
** *** * * * * *** * ** * *** * * *****
* ** ** ** * * **** ***** * ** * * **** * **
** ** ** * * * * * ** *

  // *** ****** * * * ***
  // printf("%d\n",b_sum);


*** ** ** **** ** ** (a_sum>b_sum)
*** * *** * * *** ** ***
****** *** * ** * ** * * * ** * ******** 1;
* ** * * ****** * *** * * *
* *** * * ** * ******** * if(a_sum==b_sum)
* * * * * * ** ** ***
* ** ** *** * * * *** * **** * * * * 0;
* ***** ** **** ** * * *
* * * * ** **
* * * ** * *
* ** *** *** **** **** * *** * * *** -1;
* * ** *** * ** ***
}

int main()
{
** *** ** *** ** ** *********
****** * * * * * * * ** ***** * **
* ** ** ***** *
* * * * * ** * ** * ** ** * *** * ** ** **** * *
*** ****** *** * ** * * * * **** * * ** * **
* * * * **** ****
//compare(A,B);
* * **** * ** * * * ** * * *
}
answered by (-336 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[100], b[100];

* * * * * * * *** %s",a,b);

** ** * * ** ** *** (strcmp(a,b) == 0)
* ** * ** ** ** ** * ****** * * * ** ** ** *****
* *** ** * * ** **** if(strcmp(a,b) <= 0)
** **** * ** * ** *** * * ********* **** * * ***
*** **** * * * * * *** *** * * ** * * ** * ** * * ** ** *** *** * * *** *
* * **** **** * * *** *** * * * * ** **** * ** *** *
* ** * * ** if(strcmp(a,b) >= 0)
* ** * * * **** ******* * * * ** ******* * *
** **** ** * *** ** ** * * **** * ** * ** * *** * ****** * * **** ** **** *** * ** *** * **
* *** * ***** * **** *** * ******* ***** ** * ** *
*** ** * *** *** ** 0;
}
answered by (-304 points)
edited by
0 0
prog.c: In function 'main':
prog.c:8:10: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat=]
  scanf("%s %s",&a,&b);
          ^
prog.c:8:13: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'char (*)[100]' [-Wformat=]
  scanf("%s %s",&a,&b);
             ^
0 0
prog.c: In function 'main':
prog.c:8:10: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'char *' [-Wformat=]
  scanf("%d %d",a,b);
          ^
prog.c:8:13: warning: format '%d' expects argument of type 'int *', but argument 3 has type 'char *' [-Wformat=]
  scanf("%d %d",a,b);
             ^
0 0
prog.c: In function 'main':
prog.c:8:18: error: expected expression before ']' token
  scanf("%s %s",a[],b[]);
                  ^
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Correct output
Case 4: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>
char A[5],B[5];


int compare(char A[5], char B[5])
{
** *** * ******** *** a_sum=0,b_sum=0,i;
* * * * * *** ****** * **** * * * * *** * * *** * *** * **
* *** *** *** ** ** * * * ***
** * * **** * * * * * * i<5; i++)
** **** * * ******
*** * *** *** *** ** * * * ** ****** ** *** * * **
** * *** ** * **** ** * * * * * ** **** ** *
**** * * *** * ** *

  // **** * * ** *** *** ** **
** * * **** * ********** * *


* ******* ** * (a_sum>b_sum)
* * ***** ***** *** * *
* *** * *** ** * * **** ** * **** * * 1;
    }
* ****** * * ** ** ** ** if(a_sum==b_sum)
* ******* * *** * * **
** ** * * * * ** * * ** *** *** * * * 0;
* * ** *** ** * ****
** * ** * * * *
* ** * * * * * * * **
* * ********** * * * **** ** ** * ** * ******* -1;
* * *** ** * ** *** *
}

int main()
{
**** ***** *** ***** * ** ***** * **
* ** * * *** ** * * * *
//compare(A,B);
** * *** * * **** * *****
}
answered by (-336 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Correct output
Case 3: Wrong output
Case 4: Wrong output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.71.255.127
©2016-2025

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
| 8.1k 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
| 6.2k 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
| 5.9k views
12,783 questions
183,442 answers
172,219 comments
4,824 users