Write a C program that takes two sets of characters entered by the user and merge them character by character.
Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle
Your program should include the following function:
void merge(char *s3, char *s1, char *s2);
The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer or shorter than the second set. The characters in s1 or s2 left after merging should be appended to the resulting string s3.
1) Assume each input is no longer than 1000 characters.
2) The merge function should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the function.
3) To read a line of text, use the read_line function (below)
int read_line(char *str, int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
{ if (i < n)
{ *str++= ch;
i++;
}
}
*str = '\0'; /* terminates string */
return i; /* number of characters stored */
Thank you very much in advance!
#include "stdio.h"
int read_lint(char* ,int);
//
declarte the function to read line
void merge(char*,char*,char*);
// declate the function to
merge
int main(void) {
int n=1000;
// set max characters n =
1000
char a1[n];
// declarate arrays
a1,a2,a3;
char a2[n];
char a3[n+n];
char *str1=a1;
// set pointers to arrays s1,s2,s3
char *str2=a2;
char *str3=a3;
merge(str3,str1,str2);
// call
the merge function.
printf("%s\n",str3);
// print
merged string
return 0;
}
void merge(char *str3,char *str1,char *str2){
int l1,l2,l3,min,i,j,n=1000;
// declare variables
printf("Enter the first set of characters:");
l1 = read_line(str1,n);
// get
input string
printf("Enter the second set of characters:");
l2 = read_line(str2,n);
// get
input string
min = l1>l2?l2:l1;
// calculate the min length of two strings
for(i=0,j=0;i<min;i++){
// until
min length
*(str3+ j++) = *(str1+i);
// add character to str3 from
str1
*(str3+ j++) = *(str2+i);
// add character to str3 from
str2
}
while(l1>i){
// if str1 has more characters add it to
str3
*(str3 + j++) = *(str1+ i++);
}
while(l2>i){
// if str2 has more characters add it to
str3
*(str3 + j++) = *(str2+ i++);
}
}
int read_line(char *str, int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
{ if (i < n)
{ *str++= ch;
i++;
}
}
*str = '\0'; /* terminates string */
return i; /* number of characters stored */
}
/* sample output
Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl defend the castle
*/
Write a C program that takes two sets of characters entered by the user and merge...
Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...
Using C(89). A fashion retailer has decided to provide discount codes to teachers and students during a promotional event. The first step is to verify the teacher/student status of people requesting discount code by their email addresses. They will check if the email addresses end with ".edu" and find the domain name if it does before further verification. Write a program to find the domain name of an email address if the domain name ends with .edu. The program displays...
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:...
Write a program that reads a sentence input from the user. The program should count the number of vowels(a,e,i,o,u) in a sentence. Use the following function to read the sentence. Should use switch statement with toupper. int read_line(char str[],int n) { int ch, i=0; while((ch =getchar()) != '\n') if(1<n) str[i++]==ch; str[i] != '\0'; return i; } output should look like: Enter a sentence: and thats the way it is! Your sentence...
This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){ char str[1000], ch; int i, frequency = 0; clock_t t; struct timespec ts, ts2; printf("Enter a string: "); gets(str); int len = strlen(str); printf("Enter a character...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...
****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of a file. The program encoded sentences in the file and writes the encoded sentences to the output file. Enter the file name: messages.txt Output: encoded words are written to file: messages.txt.swt The program reads the content of the file and encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i, with the letter with alphabetical position 25...
C Language! Please, comment what each line of code does in the code below and answer this question: (sentence is fine) 1. Aside from pressing ctrl-(d/z) at the beginning of a line causing the getnchar() function to return NULL, is there any way to enter less than 9 characters? Code: void exer1(void) { char input[LEN]; char *check; getchar(); // Clearing character buffer. printf("Please enter 9 characters: "); // Prompting the user to enter // a specific input. ...
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...