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:...
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...
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()...
For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words)...
Python 8.17 LAB: Strings of a Frequency Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT