tags:

views:

117

answers:

6

Say I have the line in this format

"word word 12 YR" or "word word 10 MO"

and I want to convert it to

char * containing either "12Y" or "10M" respectively.

The format is two words followed by numerical followed by the word denoting the year or the month. words are space/tab separated. Currently, I am playing around with the strtok function

Thanks

A: 

Tokenize it with space as the delimiter. Concatenate the second-to-last token, and a space, and the first character of the last token.

Dave Costa
A: 

Scan your string for spaces. Throw away the first 2 words. Combine the 3rd term with the first letter of the 4th term. strtok or just a forloop and some counts would take care of this.

Michael Dorgan
would you illustrate this with the code example? thank
vehomzzz
A: 

You could use the PCRE library and do it with a regexp.

Paul Rubel
If it's C++, he could also use boost::regex, it has the advantage to be included in the next release of the C++ standard.
Matteo Italia
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. ( http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html )
pmg
I cannot use this sort of libraries, nor boost. Let's use what's available within the standard
vehomzzz
+1  A: 

One way to do this is (no error checking, using boost):

  string s = "word word 10 MR";
  string res;
  tokenizer<> tok(s);
  tokenizer<>::iterator iter = tok.begin();
  while(iter != tok.end())
  {
      try
      {
        int n = lexical_cast<int>(*iter);
      }
      catch(bad_lexical_cast& e)
      {
          ++iter;
          continue;
      }

      break;
  }

  res = *iter;
  ++iter;
  res = res + (*iter)[0];

Non-boost solution:

  using namespace std;
  string s = "word word 10 MR";
  string res;

  stringstream ss(s);
  istream_iterator<string> iter(ss);
  istream_iterator<string> end;

  while(iter != end)
  {
      istringstream iss(*iter);
      int n;
      iss>>n;

      if( ! iss.fail())
      {
          break;
      }
      ++iter;
  }

  res = *iter;
  ++iter;
  res = res + (*iter)[0];
Naveen
+1 for using `std::string` when the OP wanted `char *`.
Thomas Matthews
+1  A: 

for ANSI C 89 have a look at:

char *str="word word 12 YR", *p=str, tmp[MAXSTR], yourstr[MAXYOURSTR]={};
...
while( 1==sscanf(p,"%s",tmp) ) {
  if( *yourstr )
  {
    yourstr[strlen(yourstr)]=*tmp;
    break;
  }
  else
  if( 1==sscanf(tmp,"%[0-9]*") )
    strcpy(yourstr,tmp);
  p+=strlen(tmp)+1;
}
A: 

user411313's answer made me think of this:

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

int main(void) {
  char result[9];
  char w[4][1000]; /* temporary */
  const char *src = "word word 12 YR"; /* "word word 10 MO"; */

  if (sscanf(src, "%s%s%s%s", w[0], w[1], w[2], w[3]) == 4) {
    size_t len;
    strcpy(result, w[2]);
    result[len = strlen(result)] = *(w[3]);
    result[len + 1] = 0;
  }
  printf("result: [%s]\n", result);
  return 0;
}
pmg