?
Solved

handling a string

Posted on 2000-02-13
2
Medium Priority
?
176 Views
Last Modified: 2010-04-02
Hi

I have a textfile containing text and numbers, like this :

Abc 34yfg 690
5 90 8
1

What I need is a short function on how to extract the numbers from this text and add them together.

The text above = 34 + 690 + 5 + 90 + 8 +1

0
Comment
Question by:Batalf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
graham_k earned 150 total points
ID: 2518026
well, I hope that it's not homework. I won't code it for you in anycase, but here's some idea.

  SInce it's a text file, use 2 nested loops.
  the outer loop reads each line of the file with gets()
   the inner loop finds each 'word' by repeatedly calling strtok()
     for each 'word' if isalpha() returns false then it's a number *
     so, add it to the total.


* is alpha will return false if you have a non-alphabetical character, this includes all digits, but also all punctuation :[]{}(),. etc  Maybe you want to write a quick isDigitsOnly()

int isDigitsOnly(char *token)
{
  for (int i = o; i < strlen(token); i++)
    if (!isdigit(token[i]))
      return 0;  // non-digit, return false

  return 1;  // all were digits, return true
}


}
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2518052
There is a simple function to test if a character is a digit, int isdigit(int thechar).
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question