Skip to content

Instantly share code, notes, and snippets.

@JeonghunLee
Last active November 16, 2016 01:48
Show Gist options
  • Save JeonghunLee/bd8d6eb6a761baee80d57f36d87adf05 to your computer and use it in GitHub Desktop.
Save JeonghunLee/bd8d6eb6a761baee80d57f36d87adf05 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
int getStrlens(char str[])
{
int cnt=0;
while(str[cnt] !='\0')
cnt++;
return cnt;
}
int main(void) {
int lens1,lens2;
char *str1="123456789"; // only read
char str2[]="my name is jeong *hun lee this-is*number 123-4568"; // read, writable
char str3[]="my name is jeong *hun lee this-is*number 123-4568"; // read, writable
char *ptr;
lens1 = getStrlens(str1);
lens2 = strlen(str1);
printf("strlens %d , %d \n",lens1,lens2);
//*(str1+1)=0; if you changed , error
/* 1. check str2 and delimeters
2. replaced only one charater in str2 with one of the delimeters
3. made 1st word and return 1st word
*/
printf("TEST1 strtok ----------------------\n");
ptr = strtok( str2, " *-");
do{
printf( "%s\n",ptr);
/* 1. check NULL and delimeters, NULL means same string.
2. replaced only one charater in str2 with one of the delimeters
3. made it 2nd word and return 2st word
*/
}while( (ptr=strtok( NULL, " *-")) );
/*
str2 was replaced with delimeters so it's different string
strtok changed NULL instead of delimeter
*/
printf( "%s\n",str2);
printf("TEST2 strpbrk ----------------------\n");
ptr = strpbrk( str3, " *-");
do{
printf("[%c]",*ptr);
printf("%s\n",(ptr+1));
}while( (ptr=strpbrk( ptr+1, " *-")) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment