Question

In: Computer Science

Write a function to concatenate two strings using pointer notation

Write a function to concatenate two strings using pointer notation

Solutions

Expert Solution

  • I assume you are C/C++ because of pointer notation.
  • Although I have tested my concatenate function in c,you can use this in c++.
  • I have passed two string pointers in function and the string after concatenation will be pointed by first pointer.
  • Rest I have tried to explain in comments.
  • Below is the whole code in c with a function call in main function.

//Code starts here

#include <stdio.h>
void concatenate_string(char *a, char *b) //Arguments are pointers to strings a and b
{
while(*a) //Loop to move "a" to end of string
a++;

while(*b) //Now we will copy 'b' characters into 'a' until end of b
{
*a = *b;   
a++;
b++;
}
//Note that pointer a will point to concatenated string
*a = '\0'; // '/0' to terminate string
}
int main()
{
char a[100], b[100]; //Defining the two strings

printf("Enter source string\n");
gets(a); //Storing a through user input

printf("Enter string to concatenate\n");
gets(b);
//This function will concatenate a and b and result will be in a
concatenate_string(a, b);
//Printing a
printf("String after concatenation: %s", a);

return 0;
}

//Code ends here


Related Solutions

using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Exercise 3 – Strings Using a function Write a program that prompts the user to enter...
Exercise 3 – Strings Using a function Write a program that prompts the user to enter two inputs: some text and a word. The program outputs the starting indices of all occurrences of the word in the text. If the word is not found, the program should output “not found”. Example1: Input1: my dog and myself are going to my friend Input2: my Output: 0 11 31 Example 2: Input1: Programming is fun Input 2: my Output: not found
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of...
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of Person pointers as a parameter. Return a pointer to the Person with the highest salary. The function must be signatured like: Person* getBestPaid(vector*); The class definition is: class Person { public: double salary; string name; }; You may include code in your main() to test, but the tests in this assignment will ensure your code is correct. You may assume there will ways be...
Write a progrm that accepts two strings as input, then indicates if the two strings are...
Write a progrm that accepts two strings as input, then indicates if the two strings are equal when the case of individual letters is ignored. Sample runs are shown below. Use C++ Enter two strings. String 1: PROgraM String 2: proGram PROgraM and proGram are equal. Enter two strings. String 1: litter String 2: LittLe litter and LittLe are not equal.
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...
Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to enter the digit code of the following text input MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY Use an inherited class using pointer variable to output the phone number from the following text input 1-800-COMCAST 1-800-VERIZON 1-800-BANCORP 1-800-MYKOHLS 1-800-JCPENNY Write C++ code and pseudocode in a doc file A computer key board has defect (like speech defect in humans) in reading for ‘p’ /’P’ as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT