Question

6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and uploaYour main program should (1) Prompt the user to enter 2 strings, and save into 2 char(40] arrays using fgets0. Assume the use

This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you

6.12 Lab Exercise Ch.6b: C-string functions Create and debug this program in Visual Studio. Name your code Source.c and upload for testing by zyLabs You will write 2 functions which resemble functions in the cstring library. But they will be your own versions 1. int cstrcat(char dstDchar src) which concatenates the char array srcl to char array dstD, and returns the new length of the dstl. not counting the null terminator character. You may assume dstl was declared large enough to handle the combined length Write the loops to find the end of the first string and to do the copy. DO NOT make calls to C-string library functions 2 int cstrcmp(char s. char t) which compares the char array s to char array tD.and returns 1 if the strings are identical, and zero otherwise. Write the loop to do the compare DO NOT make calls to C-string library functions. Be sure you don't compare past the end of either string.
Your main program should (1) Prompt the user to enter 2 strings, and save into 2 char(40] arrays using fgets0. Assume the user will enter less than 40 characters for each string. NOTE THAT YOUR PROGRAM WILL HAVE TO REMOVE THE NEWLINE CHARACTER FROM THE END OF EACH STRING, as fgets0 puts the newline in the string as its last character. You can do that by overwriting the newline char, which is at index strlen(stringname)-1, with \0' Ex: Enter 2 strings on separate lines: Hello, world! Hello there. (2) Use the cstrcmp0 function to determine if they are equal, and print out a statement (1pt): Ex: The strings are NOT equal. 3) Call the cstrcat0 function to append the 2nd string to the first, then print out the new longer string and its length (1 pt Ex: Concatenated string:Hello, world!Hello there. Length: 25
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int cstrcat(char dst[], char src[]) {
int i=0;
for(i=0;dst[i]!='\0';i++) {
}
for(int j=0;src[j]!='\0';j++) {
dst[i++]=src[j];
}
dst[i]='\0';
return i;
}
bool cstrcmp(char s[], char t[]) {
if(strlen(s) != strlen(t))
return false;
for(int i=0;s[i]!='\0' && t[i] != '\0';i++) {
if(s[i] != t[i])
{
return false;
}
}
return true;
}
int main()
{
char s1[100], s2[100];
cout << "Enter 2 strings:" << endl;
cin.getline(s1, 100);
cin.getline(s2, 100);
if(cstrcmp(s1,s2)) {
cout<<"The strings are equal."<<endl;
} else {
cout<<"The strings are NOT equal."<<endl;
}
int count = cstrcat(s1,s2);
cout<<"Concatenated string: "<<s1<<endl;
cout<<"Length: "<<count<<endl;
return 0;

}

=============
See Output

Add a comment
Know the answer?
Add Answer to:
This program should be run on Visual Studio. Please use printf and scanf as input and output. Tha...
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
  • This program should be run on Visual Studio. Please use printf and scanf as input and...

    This program should be run on Visual Studio. Please use printf and scanf as input and output. Thank you 6.11 Lab Exercise Ch.6a: Functions: String analyzer Create and debug this program in Visual Studio. Upload your Source.cpp file for testing (1) Prompt the user to enter a string of their choosing. Output the string. (1 pt) Ex: ics Enter a sentence or phrase: The only thing we have to fear is fear itself. You entered: The only thing we have...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Please help me with those 2 question problem, please help, thanks!! Code (Please use visual studio):...

    Please help me with those 2 question problem, please help, thanks!! Code (Please use visual studio): #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify...

  • This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to...

    This C++ Programs should be written in Visual studio 2017 PROGRAM 1: Comparing Using Arrays to Using Individual Variables One of the main advantages of using arrays instead of individual variables to store values is that the program code becomes much smaller. Write two versions of a program, one using arrays to hold the input values, and one using individual variables to hold the input values. The programs should ask the user to enter 10 integer values, and then it...

  • Working on a program where my task is to implement a library (as a set of...

    Working on a program where my task is to implement a library (as a set of source and header files, documentation, and a Makefile) that provides a few useful functions for working with a counted string data structure. And one of the functions that needs to be written is described below but I am having a little difficulty writing it. Program needs to be written in C. void kstrcat(kstring *destp, kstring src) Concatenates str onto the end of *destp. First...

  • Write a program that takes in a line of text as input, and outputs that line...

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH уен Hint: Use the fgets function to read a string with spaces from the user...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and...

    MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and print a simple checksum for each string. Make your string long enough to hold 50 characters. Don't forget to leave space for the null byte. Our checksum algorithm will produce a value which you can print with the syscall for printing a character. Stop reading strings when the user enters ".". The syscall to read a string (sycall code 8) adds a newline 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