Compare two strings and return the result (task on the machine in week 17th)

Source: Internet
Author: User

Use array name as parameter

Code:

/** Copyright (c) 2012, School of Computer Science, Yantai University * All Rights Reserved. * file name: test. CPP * Author: Fan Lulu * Completion Date: July 15, December 20, 2012 * version: V1.0 ** input Description: none * Problem description: Use array name as a parameter to compare two strings * program output: comparison result * Problem Analysis: * Algorithm Design: omitted */# include <iostream> using namespace STD; int astrcmp (const char str1 [], const char str2 []); int main () {char str1 [50], str2 [50]; int I; cout <"Enter the first string str1:"; CIN> str1; cout <"enter the second string str2:"; CIN> str2; I = astrcmp (str1, str2); if (I = 0) Cout <"two strings are equal. "<Endl; if (I <0) cout <" str1 greater than str2. "<Endl; if (I> 0) cout <"str2 greater than str1." <Endl; return 0;} int astrcmp (const char str1 [], const char str2 []) {int M, I = 0; for (m = 0; str1 [m] = str2 [m] & str1 [m]! = '\ 0'; m ++) // starts when two strings are not equal. I ++; If (str1 [I]> str2 [I]) return 1; if (str1 [I] <str2 [I]) Return-1; if (str1 [I] = str2 [I]) return 0 ;}

Result:

 

Use pointers as parameters

Code:

/** Copyright (c) 2012, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Fan Lulu * Completion Date: July 15, December 20, 2012 * version: v1.0 ** input Description: none * Problem description: Use Pointer parameters to compare two strings * program output: comparison result * Problem Analysis: * Algorithm Design: omitted */# include <iostream> using namespace std; int pstrcmp (const char * str1, const char * str2 ); int main () {int I; char str1 [50], str2 [50]; cout <"Enter str1:"; cin> str1; cout <"Enter str2:"; cin> str2; I = pstrcmp (str1, str2); if (I> 0) cout <"str1> str 2 "<endl; if (I <0) cout <" str1 less than str2 "<endl; if (I = 0) cout <"str1 = str2" <endl; return 0;} int pstrcmp (const char * str1, const char * str2) {int m, n = 0; for (m = 0; str1 [m] = str2 [m] & str1 [m]! = '\ 0'; m ++) n ++; if (str1 [n] = str2 [n]) return 0; if (str1 [n] <str2 [n]) return 1; if (str1 [n]> str2 [n]) return-1 ;}

Result:

 

 

Experience:

How is the output result when the pointer is used as a parameter opposite to the parameter with array name? Please explain !!!!!!!!!!!!!!!!!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.