Iv been working on this code and playing around with it. So far, i have a code for converting a string to integers.

#include <string.h>
#include <stdio.h>

char string[] = "6565,4325,7074,7897,7685,3478";
char seps[]   = " ,";
char *token;
void main(void)
{
	printf( "%s\n\n Tokens:\n", string);
	token = strtok( string, seps );
	while( token != NULL )
	{
		printf( "%s\n", token );
	token = strtok( NULL, seps );
	}
}

Now what i want to do is for the code to display or target any number that is between plus or minus 50 of 7074 or any constant number.

Any ideas? Thanks!

You need to convert the tokens to integer values before you can do an easy range check on them:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define RANGE 50

int main ( void )
{
  char string[] = "6565,4325,7074,7897,7685,3478";
  const char *seps = " ,";
  char *token;
  int target;

  printf ( "Target number: " );
  fflush ( stdout );

  if ( scanf ( "%d", &target ) != 1 ) {
    fputs ( "Invalid target", stderr );
    return EXIT_FAILURE;
  }

  printf ( "%s\n\nTokens +/- %d of %d:\n", string, RANGE, target );

  token = strtok ( string, seps );

  while ( token != NULL ) {
    int x = atoi ( token );

    if ( target > x - RANGE && target < x + RANGE )
      printf ( "%s\n", token );

    token = strtok ( NULL, seps );
  }

  return EXIT_SUCCESS;
}

If you don't know for sure whether the tokens are valid integers or not, don't use atoi. Use strtol instead because it has the capacity for good error checking while atoi just bites you in the ass.

Member Avatar for iamthwee

Iv been working on this code and playing around with it. So far, i have a code for converting a string to integers.

#include <string.h>
#include <stdio.h>

char string[] = "6565,4325,7074,7897,7685,3478";
char seps[]   = " ,";
char *token;
void main(void)
{
	printf( "%s\n\n Tokens:\n", string);
	token = strtok( string, seps );
	while( token != NULL )
	{
		printf( "%s\n", token );
	token = strtok( NULL, seps );
	}
}

Now what i want to do is for the code to display or target any number that is between plus or minus 50 of 7074 or any constant number.

Any ideas? Thanks!

I'm confused here.
http://www.daniweb.com/techtalkforums/thread38247.html

R U using c or c++? Make up ur mind? Just because sum1 has showed u how to tokenise an expression in C doesn't meant u can't do the same in C++. :confused:

So which language is it gonna B?

If you're using C++, you need a suitably complicated C++-style solution. ;)

#include <sstream>
#include <string>
#include <vector>

class Tokenizer {
public:
  explicit Tokenizer ( const std::string& s, char delim = ' ' );
  explicit Tokenizer ( const std::string& s, const std::string& delim );
public:
  std::string next() { return !done() ? *current++ : std::string(); }
  bool done() const { return current == tokens.end(); }
private:
  std::vector<std::string>           tokens;
  std::vector<std::string>::iterator current;
};

Tokenizer::Tokenizer ( const std::string& s, char delim )
{
  std::istringstream grabber ( s );
  std::string        token;

  while ( getline ( grabber, token, delim ) ) {
    if ( !token.empty() )
      tokens.push_back ( token );
  }
  current = tokens.begin();
}

Tokenizer::Tokenizer ( const std::string& s, const std::string& delim )
{
  std::string            token;
  std::string::size_type front = 0;
  std::string::size_type back = 0;

  while ( true ) {
    if ( back == std::string::npos )
      break;
    front = s.find_first_not_of ( delim, front );
    if ( front == std::string::npos )
      break;
    back = s.find_first_of ( delim, front );
    token = s.substr ( front, back - front );
    tokens.push_back ( token );
    front = back + delim.length();
  }
  current = tokens.begin();
}

#include <iostream>

int main()
{
  std::string s = "6565,4325,7074,7897,7685,3478";
  Tokenizer t ( s, ',' );

  while ( !t.done() )
    std::cout<< t.next() <<'\n';
}

You know how to convert a string to an integer, and if you hide my old Tokenizer class in a library, the solution would be simple and elegant, no?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.