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

Complete the program below in order to make run properly by competing validName methods and add...
Complete the program below in order to make run properly by competing validName methods and add trycatch block in the main method. public class ExceptionWithThrow { public static String validName(String name) throws InputMismatchException{ // check here if the name is a valid name // through InputMismatchException if invalid //throw // return the name if it is valid } public static void main(String[] args) { // Ask the user to enter a name and their validity through validName method // Add...
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
Write a Python program containing a function named scrabble_sort that will sort a list of strings...
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT