Question

In: Computer Science

converting strings to floats without using stdlib.h or string.h

converting strings to floats without using stdlib.h or string.h

Solutions

Expert Solution

#include<stdio.h>
float stofloat(char *);

int main() {
char ch[20];// take maximum 19 length string
float i;
printf("\n enter the number as a string\n");
scanf("%[^\n]", ch);//*take a string with digit like float value
i = stofloat(ch); // in this function passing base address of the string
printf(" string =%s , float =%g \n", ch, i); // print orginal string and converted float value

return 0;
}

float stofloat(char *p) // p is a pointer which holds base address of ch string and return float value

{
// take two variables for counting the number of digits in mantissa
int i, num = 0, num2 = 0, pnt= 0, x = 0, y = 1;
float f1, f2, f3;
for (i = 0; p[i]; i++) //this loop will continue up to decimal point
if (p[i] == '.') {
pnt = i;
break;
}
for (i = 0; p[i]; i++) // this loop will cotnue up to end of the string

{
if (i < pnt)

num = num * 10 + (p[i] - 48);
else if (i == pnt)

continue;
else {
num2 = num2 * 10 + (p[i] - 48);
++x;
}
}
// it takes 10 if it has 1 digit ,100 if it has 2 digits in mantissa
for (i = 1; i <= x; i++)
y = y * 10;
f2 = num2 / (float) y;
f3 = num + f2;
return f3;
}

output1:

enter the number as a string                                                                                                 

005.549                                                                                                                       

string =005.549 , float =5.549  

output2:

enter the number as a string                                                                                                 

3.34                                                                                                                          

string =3.34 , float =3.34                                                                                                   

                           


Related Solutions

#include <chrono.h> #include <stdlib.h> #include <iostream.h> #include<conio.h> #include <iomanip.h> //#include <string.h> #include <fstream.h> //using namespace std;...
#include <chrono.h> #include <stdlib.h> #include <iostream.h> #include<conio.h> #include <iomanip.h> //#include <string.h> #include <fstream.h> //using namespace std; int main(int argc, char * argv[]) { ifstream myfile; myfile.open("Input.txt"); auto st=high_resolution_clock::now(); float sum = 0; float a=0; //myfile >> a; int i=0; while (!myfile.eof()) { myfile >> a; sum+=a; } myfile.close(); auto stp=high_resolution_clock::now(); auto dur=duration_cast<microseconds>(stp-st); cout<<"Total time taken ="<<dur.count()<<"ms"<<endl;// displaying time taken cout<<"Sum of integers: "<<sum<<endl; // displaying results getch(); return 0; } can someone please tell me what is wrong with this...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to rewrite the functions linked in the code: "return add_move_lib ( board, col, colour ) ;" "return board_full_lib ( board ) ;" "return display_board_lib ( board ) ; // TASKS // board_full() display_board() add_move() // adds a token of the given value (1 or 2) to the board at the // given column (col between 1 and COLS inclusive) // Returns 0 if successful, -1...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char* str, int iPos){ if(iPos > strlen(str)||iPos < 0)return (char*)NULL; char* substr; substr = &str[iPos]; return substr; } int charPosition(char* str, char c){ char* string = (char*)malloc(strlen(str)+1); int i; for(i = 0; i < strlen(str)+1; i++) { if(str[i] == c) { return i; } } return -1; } Strings.h: #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position....
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; };...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; }; int n; void accept(struct Bank_Account_Holder[], int); void display(struct Bank_Account_Holder[], int); void save(struct Bank_Account_Holder[], int); void load(struct Bank_Account_Holder[], int); int search(struct Bank_Account_Holder[], int, int); void deposit(struct Bank_Account_Holder[], int, int, int); void withdraw(struct Bank_Account_Holder[], int, int, int); int lowBalenquiry(int,int); void main(void) { clrscr(); struct Bank_Account_Holder data[20]; int choice, account_no, amount, index; printf("NHU Banking System\n\n"); printf("Enter the count of records: "); scanf("%d", &n); accept(data, n); do {...
Change the code to sort list of strings without using any pre-defined functions Here is my...
Change the code to sort list of strings without using any pre-defined functions Here is my code: int n; String temp; Scanner in = new Scanner(System.in); System.out.print("Enter number of names you want to enter:"); n = in.nextInt(); String names[] = new String[n]; Scanner s1 = new Scanner(System.in); System.out.println("Enter all the names:"); for(int i = 0; i < n; i++){ names[i] = s1.nextLine(); } ***CHANGE THIS PART*** for (int i = 0; i < n; i++){ for (int j = i...
C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like...
C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like this alphabetically? vector words {"September", "test" "seven", "apple"} The output should be: "apple, seven, September, test"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT