Wednesday, August 12, 2009

String operation in C/C++/Java -- strtok strspn

/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* a sample program that prints the index of the first
character in the string pointed to by str1 that does not
match any of the characters in str2.*/

#include "stdafx.h"
#include
#include

int main()
{
//unsigned int len;

//len = strspn("this is a test","siht ");
//len = strspn("this is a test","se");
//printf("%d\n",len);


char string []="abc def ghi";
char * word;

word=strtok(string, " ");

printf("%s\n",word);

//word=strtok(NULL, " ");
word=strtok(string, " ");

printf("%s\n",word);



}






/* STRSTR.C */

#include
#include

char str[] = "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";

void main( void )
{
char *pdest;
int result;
printf( "String to be searched:\n\t%s\n", string );
printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
pdest = strstr( string, str );
result = pdest - string + 1;
if( pdest != NULL )
printf( "%s found at position %d\n\n", str, result );
else
printf( "%s not found\n", str );
}


Output

String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890

lazy found at position 36


String Manipulation Routines

See Also strcspn, strcmp, strpbrk, strrchr, strspn

No comments: