Solved

handling a string

Posted on 2000-02-13
2
170 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
2 Comments
 
LVL 6

Accepted Solution

by:
graham_k earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now