The following code I have written is supposed to check if the first character of the first given command line argument is the same as the last character of the last command line argument. However, I keep getting false when I am running it. Why is this occurring? #include <iostream>#include <string>using namespace std; #define N 1000   int checkChar(char str[]) { //method to check first and last char in string int n = strlen(str); char first = str[0], last = str[n - 1]; //if first equals to last then returns 1 if (first == last) return 1; //otherwise 0 else return 0; } //end of checkChar int main(int argc, char* argv[]) { //main method int check; //check variable which stores either 0 or 1 // new str string with initial size N char str[N]; for (int i = 1; i < argc; i++) { //for-loop to copy argv array element in str char type array //strcat_s(str, argv[i]);str[i] = *argv[i]; } //calling method checkChar by passing argument str check = checkChar(str); if (check) // when check have 1 (both are same) cout << "true\n"; else //when check have 0 (both are not same) cout << "false\n"; return 0; } //end of main

Question

The following code I have written is supposed to check if the first character of the first given command line argument is the same as the last character of the last command line argument. However, I keep getting false when I am running it. Why is this occurring?

#include <iostream>
#include <string>
using namespace std;

#define N 1000

 

int checkChar(char str[]) { //method to check first and last char in string

int n = strlen(str);

char first = str[0], last = str[n - 1];

//if first equals to last then returns 1

if (first == last) return 1;

//otherwise 0

else return 0;

} //end of checkChar

int main(int argc, char* argv[]) { //main method


int check; //check variable which stores either 0 or 1

// new str string with initial size N

char str[N];

for (int i = 1; i < argc; i++) { //for-loop to copy argv array element in str char type array

//strcat_s(str, argv[i]);
str[i] = *argv[i];

}

//calling method checkChar by passing argument str

check = checkChar(str);

if (check)

// when check have 1 (both are same)

cout << "true\n";

else

//when check have 0 (both are not same)

cout << "false\n";

return 0;

} //end of main

Expert Answer

Want to see the step-by-step answer?

Check out a sample Q&A here.

Want to see this answer and more?

Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Tagged in
Engineering
Computer Science

Related Computer Science Q&A

Find answers to questions asked by students like you.

Q: Python 3.7.4 Write the definition of a function twice, that receives an int parameter and returns an...

A: Program code:

Q: Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Ex: ...

A: To add code segments in the below given program code, so that the code will print "Censored" if user...

Q: in python This program requires two functions. In the main function: create an empty list. use a lo...

A: Programming instructions: First, make a list with no values.Then, utilize for-loop to add values in ...

Q: Form boolean equations in POS and SOP forms from the truth table attached (PLEASE EXPLAIN ALL STEPS)...

A: The Boolean expression from the truth table can be converted into SOP form by:In the final equation ...

Q: 3. Use a trace table to show what is the output of the following program? #include using namespace ...

A: As per the policy we are required to answer only one question.Solution for question 3 is as follows:...

Q: PLEASE EXPLAIN ALL STEPSDevelop a k-map to simplify the equation (truth table included)

A:  Given boolean expression = xz + (xy + ~z)Now by using given truth table, draw a K-map of 8 cells by...

Q: Python 3.7.4 Given that add, a function that expects two int parameters and returns their sum, and g...

A: The statement is given below,eurasia_sales = add(euro_sales, asia_sales)

Q: Hello! I am new to Java and have nearly finished up a project involving a mortgage table, and everyt...

A: According to the question, students need only in output spacing.To display output in proper spacing ...

Q: Suppose you have two columns, [Text1] and [Text2], in a MS Access table, how do you enforcethat only...

A: Validation rule to have value in one column:[Text1] and [Text2] are the two columns in a MS access t...

Q: CLASS OF 2019 Welcome MySAIL Oakland Un X Th Course: CSI-1420-43702.201940 X https://moodle.oakland....

A: Create a main method in the C program and read input from the user. And subtract the second time, wh...

Q: Programming Question: Write a program in MIPS assembly language that asks the users to enter a value...

A:  Code which is written in MIPS assembly language is shown below-

Q: PHP  Think of one of your Math homework. How can you use program to reveal the answer? Write a calcu...

A: Program Instructions:The given program implements the Strassen’s method of multiplication between tw...

Q: What is the name of a domain controller on which changes can't be written? Group of answer choices ...

A: Domain Controller:A domain controller is a server that responds to the demand for security authentic...

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: Following are the steps to create the java application Rooter that simulates the working of ATM:Crea...

Q: Python 3.7.4: Write a loop that reads positive integers from standard input and that terminates when...

A: Following is the program code that takes input from the user all positive integer and terminates if ...

Q: In MS Access, which kind of values will be accepted by the databasethrough input mask&gt;L0L0L0;0;#A...

A: An input mask in MS-Access contain 3 fields.These fields are separated by semi-colonFirst field spec...

Q: I need help with this programming exercise:   Write a C++ console application that allows your user ...

A: Program code #1:

Q: what is a router?

A: Router:A router is a physical or virtual tool for receiving, processing and transferring incoming pa...

Q: Draw the truth tables for the following Boolean expressions. xy+yz+xz x¯y+yz¯+x¯z (x⊕y)+(y⊕z)+(x...

A:  The logic gates are basically an elementary building block of the digital circuits. Logic gates are...

Q: Write a simplified expression for the Boolean function defined by the following Kmap.

A: Identify only 1’s in the kmap group.Find the largest group of 1’s.In three varible a group can have ...

Q: program7_2.pyThis Pthon program requires two functions. In the main function: create an empty list....

A: To write a program in python, that accepts the twelve random integers in a list, each in the range 1...

Q: Assume each bank branch can issue loan independently of other branches and assumecustomers may joint...

A: Option “B” is the correct functional dependency for the given table “Loans”.

Q: What is out put?

A: Output:Output is the value produced on the system based on the input supplied by the user.

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: Program InstructionsFirst, click on the file and create a new project called “PatternMaker”.Then, cr...

Q: C++ Language

A: Create the main method in the C++ program and define all required variables to solve the problem.ini...

Q: Hi I am coding for java where I have to have 20 fibonacci numbers printed out 5 in a row. Then I nee...

A: In the given code, the statement “if(count==5)” is never gets “true” because , after printing each F...

Q: Write an application starting with ProjectTemplate. Name the class AgeCalcApp. The app needs to prom...

A: To write an application name ProjectTemplate with the class name AgeCalcApp. This application prompt...

Q: I need some help arranging these countries in a C++ program : China: 1,433,783,686 India: 1,366,417,...

A: Program AlgorithmDeclare two arrays named country and population of size 14.Using for loop enter the...

Q: The current deficit of Arrakis (a fictional country) is 47000000 grods (a fictional currency). It is...

A: Program Algorithm-STARTStep1: Define a class Calculate.Step2: Write the main function for the class....

Q: Describe the activities that occur during project development.

A: Project development is a process in which resources are planned, organized and coordinated to achiev...

Q: Active Directory metadata describes the actual Active Directory data, not the Active Directory datab...

A: Active Directory:The Microsoft Technology used to manage computers and the other devices in a networ...

Q: A palindromic prime is any prime number that is also a palindrome. For example, 131 is a palindromic...

A: ·         Create a main class and required variable to solve the problem of palindromic prime.·     ...

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: Launch the NetBeans IDE.

Q: Complete the program used on the instructions given in the comments:   C++ lang #include &lt;string&...

A: Click to see the answer

Q: Python 3.7.4 Assume that print_todays_date is a function that uses no parameters and returns no valu...

A: A statement that calls (invokes) the function print_todays_date is: print_todays_date()This function...

Q: Use the Design Recipe to write a function called running_average that repeatedly asks the user to in...

A: DESIGN RECIPE:Based on the given problem use meaningful variable and function name. And perform the ...

Q: Create a GUI interface for a currency converter - Dollar-to-Euro. It should look something like the ...

A: To create a GUI converter that converts the currency from dollar to euro below is the approach and j...

Q: Please explain all steps: Exercise 2: A) Suppose the main memory of the Pep/9 were completely filled...

A: A) The Pep/9 has 64K or 65,536 of bytes in the main memory and a unary instruction takes 1 byte. Hen...

Q: Describe a cloud storage application and explain the advantages and disadvantages of such cloud stor...

A: Cloud storage:Cloud Storage is a service where data is remotely maintained, managed, and backed up. ...

Q: Create a project called Mileage. In it, create a class Car with the following properties. A car has ...

A: Program:package Mileage;public class Car {private double fuelLevel;private double fuelEfficiency;pub...

Q: Write a simplified expression for the Boolean function defined by each of the following Kmaps.

A: a)Identify only 1’s in the kmap group.Find the largest group of 1’s, group can have either 2,4 or 8....

Q: b. The birthday paradox asks how many people should be in a room so that the chances are better than...

A: There are 365 days in a year so there is 365 day’s birthday.Probability for the same birthday is p(s...

Q: Programming Problem 1Write a program that has the user input their height in inches and convert it t...

A: Program Algorithm-STARTStep1: Import the essential packagesStep2: Define a class ConvertStep3: Decla...

Q: in python   Write a program that prompts the user for three keyboard inputs and stores them in appro...

A: Following is the python program which takes three inputs from the user i.e. first name, last name an...

Q: Write an expression using membership operators that prints "Special number" if special_num is one of...

A: Python Program:#Declare and initialize the variablespecial_list = [-99, 0, 44]#Prompt the user to gi...

Q: The essential condition of third normal form is automatically satisfied whenA) a table has only one ...

A: To solve this question a user have to know the definition of Third Normal Form (3NF):Third Normal Fo...

Q: Design a reasonably efficient algorithm for solving each of the following problems and determine its...

A: Instructions:Initialize 50 state counters Count[] to zero.Traverse the given list of n student recor...

Q: This program is written in pseudocode."Design a program that generates a 7-ddigit lottery number. Th...

A: As you have not specified the programming language, we have solved this question using C++ programmi...

Q: What is single loss expectancy? What is annualized loss expectancy? What is wrong with these risk me...

A: Single loss expectancy is related to risk management and is considered as the value related to money...

Q: Think about your own university or college and choose an idea that could improve student satisfactio...

A: I passed from state university named ‘WBTU’.The university provide online facilities for students to...