Question

In: Computer Science

Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print...

Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print an error message. Also have to make sure 2nd input is an integer and not a string by default. The function in the program should take the second string and insert it into the first string in the position indicated by the integer input given. The program cannot use strlen() or strncpy() functions and it should check the integer input to make sure it is in the bounds of the length of the first string or else it should give an error.

My current solution:

#include <stdio.h>


int main()
{

int num, l1, l2;
char* s1[], s2[];
char* test[];

printf("Please enter a string, integer, and a string: ");
scanf("%s %d %s", s1, &num, s2);

while (s1[i] != '\0')
{
   l1++;
}

while (s2[i] != '\0
   l2++;
}


result = (char*) malloc (sizeof(char) * (l1 + l2));

if(result == NULL)
{
   printf("Error allocating memory");
   exit(1);
}


for (int i = 0; i < num; i++)
{
   result = s1[i];  
}

for (int i = num; i < (l1+l2); i++)
{
   result = s2[i];  
}

printf("\n\nFinal string is: %s", result);

free(result);

return 0;

}

Solutions

Expert Solution




#include <stdio.h>
#include <stdlib.h>


// Returns int value if s is a number else -1
int isNumber(char *s) {
        int value = 0;
        int i = 0;
        while(s[i] != 0) {
                if (s[i] <= '9' && s[i] >= '0') {
                        value = 10 * value + (s[i] - '0');
                } else {
                        return -1;
                }
                i++;
        }
        
        return value; 
} 

int main() {

int i, num, l1=0, l2=0;
char s1[100], s2[100], s3[100];
char test[100];

// read all input as string.
printf("Please enter a string, integer, and a string: ");
scanf("%s %s %s", s1, s2, s3);

num = isNumber(s2);

// check if s2 is number
if(num == -1) {
        printf("invalid second number %s\n", s2);
        return 1;
}
i=0;
while (s1[i] != '\0') {
   l1++;
   i++;
}
i=0;
while (s3[i] != '\0') {
   l2++;
   i++;
}

if(num > l1) {
        printf("invalid position given.\n");
        return 1;
}

char *result = (char*) malloc (sizeof(char) * (1 + l1 + l2));

if(result == NULL) {
   printf("Error allocating memory");
   exit(1);
}

for (int i = 0; i < num; i++) {
   result[i] = s1[i];
}
for (int i = 0; i < l2; i++) {
   result[num + i] = s3[i];  
}
for (int i = num; i < l1; i++) {
   result[l2 + i] = s1[i];  
}
result[l1 + l2] = '\0';

printf("\n\nFinal string is: %s", result);

free(result);

return 0;

}


Related Solutions

IN PYTHON Given a number of petals, print a string which repeats the phrases "Loves me"...
IN PYTHON Given a number of petals, print a string which repeats the phrases "Loves me" and "Loves me not" for every alternating petal. The last phrase printed (for the last petal picked) should be in all caps. Remember to include a comma and space between phrases, as shown in examples below. Sample Input 1 3 Sample Output 1 "Loves me, Loves me not, LOVES ME" Sample Input 2 1 Sample Output 2 "LOVES ME"
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
Write a program that inputs a string that represents a binary number. The string can contain...
Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console. Examples of...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer)...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer) Class(name: string, meets at: string, room: string, fid: integer) Enrolled(snum: integer, cname: string) Faculty(fid: integer, fname: string, deptid: integer) The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. Write the following queries in Oracle SQL. No duplicates should be printed in any of the answers. i) (2 points)...
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
Take an unknown number of integers as input and print the second smallest number. If there...
Take an unknown number of integers as input and print the second smallest number. If there is no second smallest number in the sequence the program should print an error message and quit. Write a function that takes a vector as argument and that returns an int containing the second smallest number. If no such number exists, the function should throw an exception that in turn is caught in main. Your program should compute its answer with a complexity no...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the telephone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The program should convert the area-code string to int and convert...
Write a function called tokenizeTelNum that inputs a telephone number as a string in the form...
Write a function called tokenizeTelNum that inputs a telephone number as a string in the form (555) 555-5555. The function should use the function strok to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The function should convert the area code string to int and...
JAVA 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation: Circumference = 2 • π • r Circle Area = π • r2 Sphere Surface Area...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT