Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

handling a string

Posted on 2000-02-13
2
Medium Priority
?
178 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

610 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