LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2005, 03:51 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
strip number from string (c code)


Hi imagin plz that we have the following string

200Kbit
300Mbit
100Kb
399Mb

k.l.p

How can i put the integer values into variables and keep the reset part of the string in other variable...
what i want is sth like

int i;
char ch[10];
i=200;
ch="Kbit";
 
Old 09-25-2005, 04:58 AM   #2
Lux Perpetua
LQ Newbie
 
Registered: Aug 2005
Location: Local Group
Distribution: Ubuntu Linux
Posts: 15

Rep: Reputation: 0
If all your strings are going to have the form "[number][unit]," then what you can do is "sscanf(str, "%d%s", &i, ch)."
 
Old 09-25-2005, 05:01 AM   #3
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
What you are looking for is:
1.) strtol (also returns a pointer to the first invalid (ie. non-numeric) character)
2.) strcpy
 
Old 09-25-2005, 07:05 AM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
thx and if i want to do the invert job?
i=20
char="k";

i want to take the string "20k"
 
Old 09-25-2005, 07:57 AM   #5
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
fprintf(3)
 
Old 09-25-2005, 04:13 PM   #6
Lux Perpetua
LQ Newbie
 
Registered: Aug 2005
Location: Local Group
Distribution: Ubuntu Linux
Posts: 15

Rep: Reputation: 0
Not quite: you are looking for sprintf, not fprintf. sscanf and sprintf are inverses of each other, roughly speaking.
Code:
sprintf(str, "%d%s", i, ch);   /* 'char' can't be the name of your string, by the way */
 
Old 09-25-2005, 05:03 PM   #7
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Oops, typo You're right.
But it leads to the same man page nevertheless

Last edited by addy86; 09-25-2005 at 05:06 PM.
 
Old 09-25-2005, 05:09 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
First they ignore you. Then they laugh at you. Then they fight you. Then you win.
Then you ignore them, then you laugh at them, then you fight with them, then you loose, then they ignore you, ... and so on
 
Old 09-26-2005, 12:47 AM   #9
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Quote:
Originally posted by jlliagre
Then you ignore them, then you laugh at them, then you fight with them, then you loose, then they ignore you, ... and so on
Let's hope that Linux will never really win and thus avoid this fatal fate.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to repeat a token/string n number of times saravkrish Programming 11 05-24-2005 01:31 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
Bash - Strip Spaces from string then cat cmfarley19 Programming 8 07-25-2004 12:01 PM
number and position of "__ " in code ... question tuxfood Programming 1 06-25-2004 09:11 AM
C; playing with string collective code TheLinuxDuck Programming 9 07-14-2003 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration