Question

In: Electrical Engineering

In C program. Read and convert a sequence of digits to its equivalent integer. Any leading...

In C program. Read and convert a sequence of digits to its equivalent integer. Any leading white space should be skipped. The conversion should include digit characters until a non-digit character is encountered. Modify the program so it can read and convert a sequence of digit characters preceded by a sign, + or -.

Solutions

Expert Solution

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

int main ()
{
   char string1[100], string2[100];
   printf("Enter the string of digits with numbers :: ");
   scanf("%s", string1);
   int i, stringLen, num = 0, j;
   stringLen = strlen(string1);
   for(i = 0, j = 0; i < stringLen; i++) {
        if (isspace(string1[i])) { }
        else if (isdigit(string1[i])) {
              string2[j] = string1[i];
              j++;
        }
       else
           break;
   }
    num = atoi (string2);
    printf ("The string value in digit is = %d", num);
}

/******************** OUTPUT OF PROGRAM ***********************
Enter the string of digits with numbers ::    502414
The string value in digit is = 502414
**************************************************************/

// Modified for + or - sign


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

int main ()
{
   char string1[100], string2[100];
   printf("Enter the string of digits with numbers :: ");
   scanf("%s", string1);
   int i, stringLen, num = 0, posflag = 0, j;
   stringLen = strlen(string1);
   for(i = 0, j = 0; i < stringLen; i++) {
        if (isspace(string1[i])) { }
        else if (string1[i] == '-') {
           posflag++;
       }
        else if (isdigit(string1[i])) {
              string2[j] = string1[i];
              j++;
        }
   }
    num = atoi (string2);
    num = posflag ? (num * (-1)) : num;
    printf ("The string value in digit is = %d", num);
}


/******************* OUTPUT OF PROGRAM *************************
Enter the string of digits with numbers ::      +102654
The string value in digit is = 102654

Enter the string of digits with numbers ::     -512415
The string value in digit is = -512415

***************************************************************/


Related Solutions

1. Write a program that read a sequence of positive integer inputs and print the sum...
1. Write a program that read a sequence of positive integer inputs and print the sum and average of the inputs. Assume that the user always enters a positive number (integer) as an input value or an empty space as a sentinel value. Please use the below to test your code [45 points]: Enter an int value or empty string to end: 98 Enter an int value or empty string to end: 78 Enter an int value or empty string...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use...
Take the Java program Pretty.java and convert it to the equivalent C program. You can use the file in.txt as sample input for your program. v import java.io.*; import java.util.*; public class Pretty { public static final int LINE_SIZE = 50; public static void main(String[] parms) { String inputLine; int position = 1; Scanner fileIn = new Scanner(System.in); while (fileIn.hasNextLine()) { inputLine = fileIn.nextLine(); if (inputLine.equals("")) { if (position > 1) { System.out.println(); } System.out.println(); position = 1; } else...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
An Armstrong number is an integer such that the sum of the cubes of its digits...
An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Write a C function to print all Armstrong numbers between a given interval. Then write a C program to keep reading two integers and print all Armstrong numbers between these two integers by calling that function.
Write a C program that allows: Three integer values to be entered (read) from the keyboard,...
Write a C program that allows: Three integer values to be entered (read) from the keyboard, Display the sum of the three values on the computer screen as follows: The integers that you have entered are: a b c The sum of a , b & c is ______ Thank you! C Code: Output screen:
SOLVE IN C: Playing with encryption: Write a program that will read a four-digit integer entered...
SOLVE IN C: Playing with encryption: Write a program that will read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the second digit with the fourth. Finally, print the original number and its encrypted one. Now reverse the process. Read an encrypted integer and decrypt it by reversing the algorithm to...
Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit...
Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit number from 31 to 99. (You do not have to check the numbers just assume the user will enter a valid number so long as you tell them to). Store an extra copy of this number for later 3.   Add 82 to the number. 4.    Remove the hundreds place from your number 5.    Add 1 to the number. 6.    Subtract this from your...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT