Jump to content

Extract Two Float/Double Numbers Out of a String on C?

TukangUsapEmenq
Go to solution Solved by TukangUsapEmenq,
10 hours ago, Unimportant said:

#include <stdio.h>

int 
main(void)
{
	const char* exampleString = "-2.2+3i";

	double real;
	double im;
	if (sscanf(exampleString, "%*[^0123456789]%lf%*[^0123456789]%lf", &real, &im) != 2)
	{
		printf("Failed to extract real & im!\n");
		return 1;
	}
	printf("real: %lf, im: %lf\n", real, im);

	return 0;
}

 

Exactly what I need. Not yet functioning as intended, but thanks a lot!

 

EDIT: got it works. Might be useful for another beginners around here.

#include <stdio.h>

int main()
{
    double real;
	double im;
    char exampleString[20];

    scanf("%s",&exampleString);


	sscanf(exampleString, "%lf+%lf", &real, &im);

	printf("real: %f, im: %f\n", real, im);

	return 0;
}

 

As the tittle says, it is. Needs you guys references for extracting two float numbers (or integers, nah any kind of number) out of a string. Example, I type -2.2+3i and I need both of the real and imaginary number to be separated so I can use them for the operations.

 

Kinda new in programming, just by 2 months learning about C on campus, so yeah.

 

I'm stuck at here searching at everywhere.

Example (for program using command line argument tho): (it's in Indonesian but... You'll get the point from the inputs and outputs.)

Spoiler

image.png.1e4e3429e8c527923af5ab8830f7bb36.png

 

Y'all help will be nicely appreciated, as deadline is 2 days away. (not only this tho, still a lot job to do)

Humor me, as you should do.

 

Daily drivers, below.

 

Diccbudd PC

Intel Xeon E3-1225 v2 || ASRock B75M Motherboard || MSI GeForce GTX 1650 Gaming X 4G || Hynix 2x8 GB DDR3 1600 MHz RAM || 480 GB Pioneer APS-SL3 SATA SSD // 1 TB Seagate 2.5" HDD || be quiet! System Power 9 500 W PSU || Cooler Master T20 CPU Cooler || Samsung S19D300 Monitor || Fantech X6 Knight Mouse || VortexSeries VX7 Pro Keyboard

 

Samsung Galaxy A34 5G

8GB RAM, 256GB Internal Storage, 128GB SanDisk Extreme, and you could find the rest of the specs on the interwebz lol

 

Lenovo ThinkPad L390 Yoga

Intel Core i5-8365U || 8 + 16 GB DDR4 (don't ask, gf bought me the 16 GB RAM as my birthday present lol) || Samsung 256GB SSD

 

Personal Server: CasaOS, Home Assistant, ESPHome, Jellyfin.

AMD E-350 || 3GB DDR3 || 120GB random SSD || 1TB Toshiba HDD

 

Audio

Redmi TV Soundbar || KZ EDX Ultra + KZ APTX Bluetooth Module || JCALLY JM6 CX31933 DAC

Link to comment
Share on other sites

Link to post
Share on other sites

You're referring to command line arguments... which can be read a certain way :

 

See https://www.geeksforgeeks.org/command-line-arguments-in-c-cpp/

 

Each argument is a separat variable, you just have to convert them to float/decimal/int/whatever using other functions ... maybe atof or stof :

http://www.cplusplus.com/reference/cstdlib/atof/

http://www.cplusplus.com/reference/cstdlib/strtof/ (may not be available in plain c)

 

also it seems optional, but you may want to convert the first argument to lowercase and then use ifs or switch ex if operation == 'add' then ...

 

if you truly mean a string, i would look for the separator between the numbers ( +,-, etc) keeping in mind there may be a + or - in the variable ex 10e+2 so maybe look for [space][operation][space]

Remember the index in the string where the operation is found, then copy the characters before into a variable, and characters after into another.. then use existing conversion functions.

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, mariushm said:

You're referring to command line arguments... which can be read a certain way :

 

See https://www.geeksforgeeks.org/command-line-arguments-in-c-cpp/

 

Each argument is a separat variable, you just have to convert them to float/decimal/int/whatever using other functions ... maybe atof or stof :

http://www.cplusplus.com/reference/cstdlib/atof/

http://www.cplusplus.com/reference/cstdlib/strtof/ (may not be available in plain c)

 

also it seems optional, but you may want to convert the first argument to lowercase and then use ifs or switch ex if operation == 'add' then ...

 

if you truly mean a string, i would look for the separator between the numbers ( +,-, etc) keeping in mind there may be a + or - in the variable ex 10e+2 so maybe look for [space][operation][space]

Remember the index in the string where the operation is found, then copy the characters before into a variable, and characters after into another.. then use existing conversion functions.

Quick one reply, thanks! That atof might be useful.

And yeah I really meant the string. Any kind of codes example for this? I learn much more easily if I read the codes directly.

Humor me, as you should do.

 

Daily drivers, below.

 

Diccbudd PC

Intel Xeon E3-1225 v2 || ASRock B75M Motherboard || MSI GeForce GTX 1650 Gaming X 4G || Hynix 2x8 GB DDR3 1600 MHz RAM || 480 GB Pioneer APS-SL3 SATA SSD // 1 TB Seagate 2.5" HDD || be quiet! System Power 9 500 W PSU || Cooler Master T20 CPU Cooler || Samsung S19D300 Monitor || Fantech X6 Knight Mouse || VortexSeries VX7 Pro Keyboard

 

Samsung Galaxy A34 5G

8GB RAM, 256GB Internal Storage, 128GB SanDisk Extreme, and you could find the rest of the specs on the interwebz lol

 

Lenovo ThinkPad L390 Yoga

Intel Core i5-8365U || 8 + 16 GB DDR4 (don't ask, gf bought me the 16 GB RAM as my birthday present lol) || Samsung 256GB SSD

 

Personal Server: CasaOS, Home Assistant, ESPHome, Jellyfin.

AMD E-350 || 3GB DDR3 || 120GB random SSD || 1TB Toshiba HDD

 

Audio

Redmi TV Soundbar || KZ EDX Ultra + KZ APTX Bluetooth Module || JCALLY JM6 CX31933 DAC

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, dhannemon13 said:

Needs you guys references for extracting two float numbers (or integers, nah any kind of number) out of a string.

#include <stdio.h>

int 
main(void)
{
	const char* exampleString = "-2.2+3i";

	double real;
	double im;
	if (sscanf(exampleString, "%*[^0123456789]%lf%*[^0123456789]%lf", &real, &im) != 2)
	{
		printf("Failed to extract real & im!\n");
		return 1;
	}
	printf("real: %lf, im: %lf\n", real, im);

	return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Unimportant said:

#include <stdio.h>

int 
main(void)
{
	const char* exampleString = "-2.2+3i";

	double real;
	double im;
	if (sscanf(exampleString, "%*[^0123456789]%lf%*[^0123456789]%lf", &real, &im) != 2)
	{
		printf("Failed to extract real & im!\n");
		return 1;
	}
	printf("real: %lf, im: %lf\n", real, im);

	return 0;
}

 

Exactly what I need. Not yet functioning as intended, but thanks a lot!

 

EDIT: got it works. Might be useful for another beginners around here.

#include <stdio.h>

int main()
{
    double real;
	double im;
    char exampleString[20];

    scanf("%s",&exampleString);


	sscanf(exampleString, "%lf+%lf", &real, &im);

	printf("real: %f, im: %f\n", real, im);

	return 0;
}

 

Humor me, as you should do.

 

Daily drivers, below.

 

Diccbudd PC

Intel Xeon E3-1225 v2 || ASRock B75M Motherboard || MSI GeForce GTX 1650 Gaming X 4G || Hynix 2x8 GB DDR3 1600 MHz RAM || 480 GB Pioneer APS-SL3 SATA SSD // 1 TB Seagate 2.5" HDD || be quiet! System Power 9 500 W PSU || Cooler Master T20 CPU Cooler || Samsung S19D300 Monitor || Fantech X6 Knight Mouse || VortexSeries VX7 Pro Keyboard

 

Samsung Galaxy A34 5G

8GB RAM, 256GB Internal Storage, 128GB SanDisk Extreme, and you could find the rest of the specs on the interwebz lol

 

Lenovo ThinkPad L390 Yoga

Intel Core i5-8365U || 8 + 16 GB DDR4 (don't ask, gf bought me the 16 GB RAM as my birthday present lol) || Samsung 256GB SSD

 

Personal Server: CasaOS, Home Assistant, ESPHome, Jellyfin.

AMD E-350 || 3GB DDR3 || 120GB random SSD || 1TB Toshiba HDD

 

Audio

Redmi TV Soundbar || KZ EDX Ultra + KZ APTX Bluetooth Module || JCALLY JM6 CX31933 DAC

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×