Question

Given two C-string s1 and s2, write an if - else if - else statement to...

Given two C-string s1 and s2, write an if - else if - else statement to print out message to indicate which string goes first in lexicographic order or two strings are the same.

It must. Be if-else if -else statement c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <iostream>

//using c string function not c++

#include<string.h>

using namespace std;

void lexical_order(char *s1,char *s2){

//using c string header function strcmp only not c++ one

int c=strcmp(s1,s2);

//strcmp returns three values 1,-1 and 0

if (c==1){

cout<<s2<<" comes first in lexicographic order"<<endl;

}

else if(c==-1){

cout<<s1<<" comes first in lexicographic order"<<endl;

}

else{

cout<<"Two strings are the same."<<endl;

}

}

int main() {

//two string function

char s1[]="Hellb";

char s2[]="Hella";

lexical_order(s1,s2);

}

Output:

Hella comes first in lexicographic order

Add a comment
Know the answer?
Add Answer to:
Given two C-string s1 and s2, write an if - else if - else statement to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming 1 String Merging Write a program that reads two strings s1 and s2 from...

    C programming 1 String Merging Write a program that reads two strings s1 and s2 from the keyboard and merges them to a string s3. Strings s1 and s2 are statically allocated whereas s3 is dynamically allocated to fit exactly s1 and s2. The two original strings are no longer than 100 characters long. Use the following function to merge the two strings. char *stringMerge(char s1[], char s2 []); Example: Enter the first string: Hello world Enter the first string:...

  • ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...

    ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. int indexOf(const string& s1, const string& s2) Write a test program that reads two strings and checks whether the first string is a substring of the second string.

  • Write a NASM assembly language(run it on DOSBOX) procedure using string instruction. Given two strings, S1...

    Write a NASM assembly language(run it on DOSBOX) procedure using string instruction. Given two strings, S1 and S2, determine if S2 is a sub string of S1. Sub string example: S1: ‘I am happy today’, S2: ‘happy’, S2 is a substring of S1. S1: ‘I am happy today’, S2: ‘sad’, S2 is not a substring of S1. Call above procedure in an assembly language program and show the result of the procedure.

  • Write the function in python and show the code 2. Write a function jscore (s1, s2)...

    Write the function in python and show the code 2. Write a function jscore (s1, s2) that takes two strings s1 and s2 as inputs and returns the Jotto score of s1 compared with s2 -.e., the number of characters in s1 that are shared by s2. The positions and the order of the shared characters within each string do not matter. Repeated letters are counted multiple times, as long as they appear multiple times in both strings. For example...

  • Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if...

    Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf. isReverse("happy", "yppah") will return true isReverse("cool", "loac") will return false isReverse("", "") will return true

  • You are given a list of strings L = {s1,s2 ...sn}. Each si is a string...

    You are given a list of strings L = {s1,s2 ...sn}. Each si is a string of up to 26 English characters. For example, L consists of the following strings: {s1, 82... Sn}. Each s; is a string of up to 26 English 13. You are given a list of of strings L = characters. For example L consists of the following strings: S1 =hello', S2 = 'world, s3 = 'what', s4 = 'a', 85 = 'nice', s6 = 'day',...

  • use C language please Problem 5. (5 points] Use string functions. Write a program that: 1)...

    use C language please Problem 5. (5 points] Use string functions. Write a program that: 1) Ask the user to write two strings. 2) If both strings are equal, print an appropriate message then print out both strings with their length. 3) If the strings are not equal, print an appropriate message then concatenate both strings and print out the length of the concatenating strings. 4) In the main function prints both arrays.

  • s1 = 'Practice How to Use String Object' Write a python statement that splits the string,...

    s1 = 'Practice How to Use String Object' Write a python statement that splits the string, creating the following list: ['Practice', 'How', 'to', 'Use', 'String', 'Object'] 2. Assume reply references a string. The following if statement determines whether reply is equal to ‘Y’ or ‘y’: reply = input("Y or y to continue: ") if reply == "Y" and reply == "y":    print("Y --- <uppercase> has been selected. ") else:    print("y --- <lowercase> has been chosen. ") print("… continue...

  • Write an if-else statement that checks patronAge. If 55 or greater print 'Senior citizen', otherwise print...

    Write an if-else statement that checks patronAge. If 55 or greater print 'Senior citizen', otherwise print 'Not senior citizen' (without quotes). End with newline. 1 import java.util.Scanner; Test pas public class DetectSenior public static void nain (String[] args) { Scanner sehr = new Scanner(System.in); int patronage; patronage - ser.nextInt(); * Your solution goes here */ 12 13 Write an if-else statement with multiple branches. If given Year is 2101 or greater print 'Distant future" (without quotes). Else, if given Year...

  • 8-7. Modify exercise 7-2 on comparing strings so it is now done in an applet instead...

    8-7. Modify exercise 7-2 on comparing strings so it is now done in an applet instead of an application. The prompts will now be in labels and the input will now be textfields. The user will hit enter in the secondtextfield to get the results in two other answer labels. The results (which is greater and the difference amount) will be in these two other answer labels. Declare everything at the top, use the init to add the components to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT