Re: split a string

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 06/14/04


Date: Mon, 14 Jun 2004 11:28:24 +0100

In article <b02d7807.0406140134.7906d43e@posting.google.com>, Ronen Kfir
<ronenk@tauex.tau.ac.il> writes
>Hi,
>
>I have a string of N characters. Inside this string planted one or
>more ‘*' characters, which are the delimiter for the substring.
>
>Example:
>
>dfka*kghe*c***mbmfkj
>
>I need to find the length of each substring. I have figured that in
>order to count the substrings I need to replace '*' with \0. How do I
>do that replacement? If I do that, how will the substrings will be
>allocated? How will I access them?
>Need to acomplish task without using slice() or any library function
>of family.

Then I think you have figured out wrong. Do not even dream of messing
with the characters. You should be able to achieve your objective with a
const qualified string (though I wish you had followed instructions and
told us whether you were writing C or C++ because the exact idiomatic
mechanisms are different in the two languages.

A very unpolished (i.e. I would give it a barely passing grade as a
teacher) solution in C is:

size_t mystrlen(char const ** source_ptr){
   int count = 0;
   char const * source = *source_ptr;
   for(count; source[count]; ++count){
     ++(*source_ptr);
     if(source[count] == '*') break;
   }
   return count;
}

-- 
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


Relevant Pages

  • Re: How does this work?
    ... Let's say the string value is ... The value of the first argument is the substring that matched. ... the match extends as long as the delimiter characters don't match. ... So, as you can probably see now, it is actually a simple template engine ...
    (comp.lang.javascript)
  • Re: How to convert Infix notation to postfix notation
    ... If this is for an error message, why isn't it using stderr for its output? ... array of 15 characters, and you call this function with the limit 15 on ... Making sure that the only string I allocate and append to, ... because mulFactor in all versions must needs incorporate the functions ...
    (comp.lang.c)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)
  • Re: "Reversed" LCS problem
    ... have to worry so much about defining "substring" versus "subsequence," ... say "the letters in the string must be next to each other, ... >> has the lowest number of common characters from the other strings, ... > No whitespaces, no sequences, only substrings. ...
    (comp.programming)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... put them in stupid places. ... Programming is difficult (as you must surely appreciate, ... > strings will be in the range 1...1000 characters. ... impose an artificially small limit on string length." ...
    (comp.programming)