Question

C code only! Complete this function so that the function str2int convert the char array s...

C code only!

Complete this function so that the function str2int convert the char array s into its equivalent integer value. The char array has only digit characters of{0, 1, .., 9}and ’\0’.And the first element is not 0, which means, the integer string in the char array is of base 10.

For example, if we pass chars[] = ”123” intostr2int, then it should return 123.Close the braces when you are done

int str2int(char s[]){

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int str2int(char s[]){
    int length = 0, i, prod = 1, result = 0;
    // calculating length of string s
    while(s[length]!='\0')
        length++;
    for(i = length-1;i>=0;i--){
        result += (prod*(s[i]-'0'));
        prod = prod * 10;
    }
    return result;
}

int main()
{
    char s[] = "123";
    int result = str2int(s);
    printf("%d",result);
    return 0;
}

123

Add a comment
Know the answer?
Add Answer to:
C code only! Complete this function so that the function str2int convert the char array s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C. // Extract the integer from the input string and convert to int int convert(char...

    In C. // Extract the integer from the input string and convert to int int convert(char *input){ // Declare int for result extracted from input int res = 0; // Declare int for sign of result // Declare two iterators // Declare a buffer for numeric chars // Set error to zero - no error found yet // Check for space in element 1 // Check for negative integer at element 2 // Loop to copy all numeric chars to...

  • 4. Write a C function int str2int(char *s) that takes string s as parameter and return...

    4. Write a C function int str2int(char *s) that takes string s as parameter and return the integer that s represents. For example, if string s is 123", then the function return integer 123.

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z')...

    Create a UNIX makefile for the following C program: //convert.c char toUpper(char c){ if(c>='a' && c<='z') return c-32; return c; } //converting sentance to upper void convertSentence(char *sentence){ int i=0; while(sentence[i]!='\0'){ //convert to upper for each character sentence[i] = toUpper(sentence[i]); i++; } } //converting all sentences into uppercase void convertAll(char **sentenceList, int numOfSentences){ int i=0; while(i<numOfSentences){ //calling convertsentence function to conver uppercase convertSentence(sentenceList[i]); i++; } } sentences.c #include<stdio.h> #include<stdlib.h> #include "convert.c" int main(){ //declaring character array char **mySentences; int noOfSentences;...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • Can you help me make these two methods listed below work? I have code written but...

    Can you help me make these two methods listed below work? I have code written but it is not working. I would appreciate any advice or help. Function "isAPalidrome" accepts an array of characters and returns an integer. You must use pointer operations only, no arrays. This function should return 1 (true) if the parameter 'string' is a palindrome, or 0 (false) if 'string' is not a palindrome. A palindrome is a sequence of characters which when reversed, is the...

  • How do I take a random char element from an existing array and assign it to...

    How do I take a random char element from an existing array and assign it to a new array? I have this code with a testing method but my code only returns an empty array instead of the chars expected. public static char[] generateHiddenCode(Random rand, int numPositions, char[] symbols) { char[] arrCode = new char[numPositions]; for (int i=0; i<arrCode.length; ++i) { arrCode[i] = (char) rand.nextInt(symbols.length); } return arrCode; } Test:    private static void testGenerateHiddenCode() { boolean error = false;...

  • Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)

    please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...

  • Need help with these array problems. int countAllPunctuation( const string array[ ], int n ); Return...

    Need help with these array problems. int countAllPunctuation( const string array[ ], int n ); Return the total number of punctuation symbols found in all the elements of the passed array argument. For the purpose of this function, the characters '.' , ',', '!', ';', ''', '-', '/', ':', '?', '"' count as punctuation symbols (that is, period, comma, exclamation mark, semicolon, apostrophe, dash, slash, colon, question mark, and double quote). Return -1 if n <= 0. For example, for...

  • Write a function named insertBeforeKey that takes the parameters as follows list as an char array,...

    Write a function named insertBeforeKey that takes the parameters as follows list as an char array, capacity for the capacity of the array, numItems has the number of items in the array, key is an element of the array before which newVal is to inserted. Assume key is always present in the array. and newVal has the new value to be inserted into the array. If the array is at capacity then the function should return -1, otherwise return 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT