Question

In: Computer Science

This program works with strings complete the following functionsto allow the program to function properly;...

This program works with strings complete the following functions to allow the program to function properly;

a) Write a C function int string_to_number(char *) that takes a string of decimal digits as an input parameter, and returns the integer value that the string represents. For example, if the char s[] ="256", then string_to_number(s) returns integer 256 (int data type). You are allowed to use strlen(…) function, but not allowed to use atoi and other functions. Ignore the negative sign and overall.

b) Write a C function void reverse(char *s) that takes a string as input, and reverses the order of its characters and stores in the string. For example, if string s is "abcd", after the function call, s becomes "dcba". You are allowed to use strlen(…) function.

c) Implement the above function without using strlen function. Just use pointer notation.

Solutions

Expert Solution

C CODE:

A.

#include
#include
int string_to_decimal(char *a,int n){
   int i,k=0;
   for(i=0;a[i]!='\0';i++){
       //logic for converting string to int
       k=k*10+a[i]-'0';
   }
   return k;
}
int main(){
   int n;
   printf("Enter the length of string\n");
   scanf("%d",&n);
   printf("Enter the string of length %d \n",n);
char s[n];  
   scanf("%s",&s);
   int res=string_to_decimal(s,strlen(s));
   printf("Number changed to int is %d",res);
   //printf("%d",res);
}

SCREENSHOT OF THE CODE AND OUTPUT:

B.

#include
#include

void reverse(char *s){
   int n,low,high;
   n=strlen(s);
   high=n-1;
   char k[50];
   for(low=0;s[low]!='\0';low++){
       k[low]=s[high];//copying into new string
       high--;
   }
   k[low]='\0';
   printf("Reversed string is %s",k);

  
}
int main(){
  
   printf("Enter the string\n");
char s[50];  
   scanf("%s",&s);//taking input
   //calling reverse function
   reverse(s);
  
}

SCREENSHOT OF THE CODE AND OUTPUT:

C.

#include
#include

int fun(char *s)
{
int i = 0;

while( *(s+i) != '\0' )
{
   i++;}

return i;
}
void reverse(char *s)
{
int n, i;
char *low, *high, temp;

n= fun(s);


low = s;
high = s;

for (i = 0; i < n - 1; i++)
{
   high++;}

for (i = 0; i < n/2; i++)
{
temp = *high;//storing anf swapping
*high = *low;
*low= temp;

low++;
high--;
}


}

int main(){
  
   printf("Enter the string\n");
char s[50];  
   scanf("%s",&s);//taking input
   //calling reverse function
   reverse(s);
   printf("Reversed string is %s",s);
}

SCREENSHOT OF THE CODE AND OUTPUT:


Related Solutions

Write a Python program that uses function(s) for the following problem: A nutritionist who works for...
Write a Python program that uses function(s) for the following problem: A nutritionist who works for a fitness club helps members by evaluating their diets. As part of her evaluation, she asks members for the number of fat grams and carbohydrate grams that they consumed in a day (two inputs, one for number of fat grams and the other for number of carb grams). Then, she calculates the number of calories that result from the fat, using the following formula:...
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
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
Develop an x86 assembly language program that properly executes an "x to the y power" function:...
Develop an x86 assembly language program that properly executes an "x to the y power" function: int power(int x, int y) Compute the integer value that is x to the y power Do not concern yourself with the possibility of overflow You may only use only a loop structure to compute the value Remember that the return value must be placed in the EAX register Make sure that any registers (not EAX) used by this function are placed back to...
Write a python program for the following question. Complete the symptom similarity function, which measures the...
Write a python program for the following question. Complete the symptom similarity function, which measures the similarity between the symptoms of two patients. See below for an explanation of how the similarity is computed. def symptom_similarity(symptoms_A: Tuple[Set], symptoms_B: Tuple[Set]) -> int: '''Returns the similarity between symptoms_A and symptoms_B. symptoms_A and symptoms_B are tuples of a set of symptoms present and a set of symptoms absent. The similarity measure is computed by the following equations: present_present + absent_absent - present_absent -...
Working with Strings The following program illustrates the use of some of the methods in the...
Working with Strings The following program illustrates the use of some of the methods in the String class. Study the program to see what it is doing. // *************************************************************** // StringManips.java // Test several methods for manipulating String objects // *************************************************************** import java.util.Scanner; public class StringManips { public static void main (String[] args) { String phrase = new String ("This is a String test."); int phraseLength; // number of characters in the phrase String int middleIndex; // index of the...
Write a complete Java program called CharCounter that gets two Strings called inputWord and inputCharacter from...
Write a complete Java program called CharCounter that gets two Strings called inputWord and inputCharacter from the user at the command line. Check that the character has a length of 1. If it doesn't, provide the user with suitable feedback and conclude the program. If the character length is valid (i.e., it has a length of 1), use a while loop to check each position in the inputWord variable and return the number of times the character occurs. For example,...
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value)...
Develop an x86 assembly language program that properly executes an "is even" function: bool isEven(int value) If the value passed in is even, return 1 If the value passed in is odd, return 0 Remember that the return value must be placed in the EAX register Make sure that any registers (not EAX) used by this function are placed back to their initial states prior to exiting Allow an end user to test this function in the following manner: Prompt...
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings will each be ONE character and will represent a nucleotide. The list will be a nested int list representing a 4x4 score matrix. This function will return the value (int) from the nested int list at the location of the two referenced nucleotides. a. An example call to calculate_score would be calculate_score(“A”, “T”, score_matrix). If we look at the alignment score table in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT