C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
strspn - String Function Posted by tokoG on 30 Mar 2006 at 11:32 PM
This message was edited by tokoG at 2006-3-30 23:34:2


Hi!

I came accross the new string function, strspn and checked what it does.
This site had pretty good explanation http://www.cplusplus.com/ref/cstring/strspn.html but, all in all, I think what it does is return the number(length) of what it matches to the other string.

In here, strtext & cset both contains 1 & 2 & 9, that's why the return is 3. Am I right?
/* strspn example */
#include <stdio.h>
#include <string.h>

int main ()
{
  int i;
  char strtext[] = "129th";
  char cset[] = "1234567890";

  i = strspn (strtext,cset);
  printf ("Length of initial number is %d\n",i);
  return 0;
}

Output:
Length of initial number is 3


I don't know how this could be useful..?




Report
Re: strspn - String Function Posted by tsagld on 31 Mar 2006 at 1:47 AM
: This message was edited by tokoG at 2006-3-30 23:34:2

:
: Hi!
:
: I came accross the new string function, strspn and checked what it does.
: This site had pretty good explanation http://www.cplusplus.com/ref/cstring/strspn.html but, all in all, I think what it does is return the number(length) of what it matches to the other string.
:
: In here, strtext & cset both contains 1 & 2 & 9, that's why the return is 3. Am I right?
:
: /* strspn example */
: #include <stdio.h>
: #include <string.h>
: 
: int main ()
: {
:   int i;
:   char strtext[] = "129th";
:   char cset[] = "1234567890";
: 
:   i = strspn (strtext,cset);
:   printf ("Length of initial number is %d\n",i);
:   return 0;
: }
: 
: Output:
: Length of initial number is 3
: 

:
: I don't know how this could be useful..?
:
:
:
:
:

From MSDN:
Returns the index of the first character in a string that does not belong to a set of characters.


In your example, the letter 't' in "129th" is the first character that does not occur in the string "1234567890".
The 't' is at position 3 (zero based) in "129th", that's why the return value is 3.

Greets,
Eric Goldstein
www.gvh-maatwerk.nl

Report
Re: strspn - String Function Posted by tokoG on 31 Mar 2006 at 8:48 PM

: From MSDN:
: Returns the index of the first character in a string that does not belong to a set of characters.
:

:
: In your example, the letter 't' in "129th" is the first character that does not occur in the string "1234567890".
: The 't' is at position 3 (zero based) in "129th", that's why the return value is 3.
:
: Greets,
: Eric Goldstein
: www.gvh-maatwerk.nl
:


Cool. Very thanks you now I get it.
I tried with the following the result was 4

char strtext[] = "129th";
char cset[] = "12t4567890";

Because 'h' is the one that doesn't occur in the other string. :)





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.