Question

What will the value of the string s1 after the following statements have been executed? strcpy(s1,...

What will the value of the string s1 after the following statements have been executed?

strcpy(s1, "computer");
strcpy(s2, "science");
if(strcmp(s1, s2) < 0){
    strcat(s1, s2);
}
else{
    strcat(s2, s1);
}
s1[strlen(s1) - 6] = '\0';

I know the answer is "Computers" but why? Can someone explain to me how to approach this problem?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
strcpy(s1, "computer"); ->  s1 is "computer"
strcpy(s2, "science");  ->  s2 is "science"
if(strcmp(s1, s2) < 0){ ->  if s1 is less than s2(alphabetically). computer is less than science because c comes before s
    strcat(s1, s2); //  s1 becomes "computerscience"
}
else{
    strcat(s2, s1);
}
s1[strlen(s1) - 6] = '\0';      // strlen(s1) is 15. 15-6=9.  so, s1[9] = '\0'.  so, s1 becomes computerscience\0ience"

all characters after \0 are ignored. because \0 indicates end of string.
that's value s1 prints  "computers"
Add a comment
Know the answer?
Add Answer to:
What will the value of the string s1 after the following statements have been executed? strcpy(s1,...
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
  • Programming In C A) What will print after the following statements execute? char s1[50] = "jack";...

    Programming In C A) What will print after the following statements execute? char s1[50] = "jack"; char s2[50] = "jill"; char s3[50]; printf("%s", strcat(strcat(strcpy(s3, s1), " and "), s2)); B) What will print after the following statements execute? char s1[50] = "jack"; char s2[50] = "jill"; char s3[50]; printf("%u", strlen(s3));

  • Problem Consider three processes A, B, and C which have statements S1, S2 and 53 respectively....

    Problem Consider three processes A, B, and C which have statements S1, S2 and 53 respectively. These statements should execute in a particular fashion. S3 should be executed only after executing S1 and S2. Statements S1 and 52 can be executed in any order. Implement the above using binary semaphore variables X1 and X2 which are initially set to zero. O A: S1; signal (x1); B: S2; signal (X2); C: wait(x1); wait (X2); S3;

  • 1. Given the string variables pres, first, and last as defined in Example 9.2 provided on...

    1. Given the string variables pres, first, and last as defined in Example 9.2 provided on page 459 of your textbook, Problem Solving and Program Design in C, show what would be displayed by this code fragment. (If you do not know, you can always place this code into the proper C coding structure and compile and execute.) Make certain your call includes <stdio.h> and <string.h> and declare your variables if you are going to compile. (Hint:Refer to the text...

  • A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase,...

    A CERTAIN program to user's password containing rules such as least n length, one uppercase, lowercase, one digit and white space is given as the code down below. So, simiiar to those rules, I need the code for the following questions post in pictures such as no more three consecutive letters of English alphabets, password should not contain User name and so on. //GOAL: To learn how to create that make strong passwords //Purpose: 1)To learn some rules that makes...

  • # After the if statement that follows is executed, what will the value of $discount_amount be?...

    # After the if statement that follows is executed, what will the value of $discount_amount be? $discount_amount; $order_total = 200; if ($order_total > 200) { $discount_amount = $order_total * .3; } else if ($order_total > 100) { $discount_amount = $order_total * .2; } else { $discount_amount = $order_total * .1; }         2         20         40         60 # To open a file in PHP, you use the _________ function.         fopen()        ...

  • Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...

    Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i<s.length(); i++){ reverse[i] = original[original.length-1-i]; } return new String(reverse); } /**  * Takes in a string containing a first, middle and last name in that order.  * For example Amith Mamidi Reddy to A. M. Reddy.  * If there are not three words in the string then the method will return null  * @param name in...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • Using Python       1. Write a function to generate an index” for below passage. Insert this...

    Using Python       1. Write a function to generate an index” for below passage. Insert this quote passage = "Writing programs or programming is a very creative and rewarding activity. You can write programs for many reasons ranging from making your living to solving a difficult data analysis problem to having fun to helping someone else solve a problem. This book assumes that everyone needs to know how to program and that once you know how to program, you will...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names,...

    Lab 2: (one task for Program 5): Declare an array of C-strings to hold course names, and read in course names from an input file. Then do the output to show each course name that was read in. See Chapter 8 section on "C-Strings", and the section "Arrays of Strings and C-strings", which gives an example related to this lab. Declare the array for course names as a 2-D char array: char courseNames[10] [ 50]; -each row will be a...

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