Solved

handling a string

Posted on 2000-02-13
2
169 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

21 Experts available now in Live!

Get 1:1 Help Now