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

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
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:
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 that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT