91
Functions on character strings
- int mystrlen1( const char *s ) {
- int i;
- for ( i = 0; s[ i ]; i++ )
- ;
- return i;
- }
- int mystrlen2( const char *s ) {
- int i;
- for ( i = 0; *s; i++ )
- ++s;
- return i;
- }
- int mystrlen3( const char *s ) {
- const char *p = s;
- while ( *s )
- ++s;
- return s-p;
- }
- #include <stdio.h>
- #include <string.h>
- main( ) {
- char *s0 = "Hello, my dear!";
- char *s1 = "";
- printf( "\"%s\"->15=%d\n", s0, strlen( s0 ));
- printf( "\"%s\"->15=%d\n", s0, mystrlen1( s0 ));
- printf( "\"%s\"->15=%d\n", s0, mystrlen2( s0 ));
- printf( "\"%s\"->15=%d\n", s0, mystrlen3( s0 ));
- printf( "\"%s\"->0=%d\n", s0, strlen( s1 ));
- printf( "\"%s\"->0=%d\n", s0, mystrlen1( s1 ));
- printf( "\"%s\"->0=%d\n", s0, mystrlen2( s1 ));
- printf( "\"%s\"->0=%d\n", s0, mystrlen3( s1 ));
- }
$ gcc -o strlen strlen.c
$ ./strlen
"Hello, my dear!"->15=15
"Hello, my dear!"->15=15
"Hello, my dear!"->15=15
"Hello, my dear!"->15=15
"Hello, my dear!"->0=0
"Hello, my dear!"->0=0
"Hello, my dear!"->0=0
"Hello, my dear!"->0=0
- char *mystrcpy( char *s, const char *s0 ) {
- char *p = s;
- while ( *s++ = *s0++ )
- ;
- return p;
- }
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- main( ) {
- char *s0 = "Hello, my copy cat!";
- char *s1 = (char *)malloc( strlen( s0 ) + 1);
- printf( "s0=%s\n", s0);
- printf( "strlen(s0)=%d\n", strlen( s0 ));
- printf( "mystrcpy(s1, s0)->0x%x=0x%x\n", (unsigned)s1, (unsigned)mystrcpy( s1, s0 ));
- printf( "s1=%s\n", s1);
- printf( "strlen(s1)=%d\n", strlen( s1 ));;
- printf( "s0=%s\n", s0);
- printf( "strlen(s0)=%d\n", strlen( s0 ));
- }
$ gcc -o strcpy strcpy.c
$ ./strcpy
s0=Hello, my copy cat!
strlen(s0)=19
mystrcpy(s1, s0)->0x87de008=0x87de008
s1=Hello, my copy cat!
strlen(s1)=19
s0=Hello, my copy cat!
strlen(s0)=19
- #include <string.h>
- char *mystrcat(char *s, const char *s0) {
- char *p = s;
- strcpy(s+strlen(s), s0);
- return p;
- }
- #include <stdio.h>
- #include <stdlib.h>
- main() {
- char *s0 = "Hello";
- char *s1 = "my copycat!";
- char *sep = ", ";
- char *s = (char *)malloc(strlen(s0) + strlen(sep) + strlen(s1) + 1);
- printf("s0+sep+s1=%s%s%s\n", s0, sep, s1);
- strcpy(s, s0);
- mystrcat(s, sep);
- mystrcat(s, s1);
- printf("s0+sep+s1=%s\n", s);
- }
$ gcc -o strcat strcat.c
$ ./strcat
s0+sep+s1=Hello, my copycat!
s0+sep+s1=Hello, my copycat!
- #include <string.h>
- int mystrcmp(const char *s1, const char *s2) {
- while ( *s1 && *s2) {
- if ( *s1 < *s2)
- return -1;
- if ( *s1 > *s2)
- return 1;
- else
- ++s1, ++s2;
- }
- /* add the following lines after discussion */
- if (*s1 == '\0' && *s2 == '\0')
- return 0;
- return (*s1 == '\0') ? -1 : 1;
- }
- #include <stdio.h>
- main() {
- char *s0 = "abc";
- char *s1 = "abC";
- char *s2 = "ABCD";
- char *s3 = "ABCDE";
- printf("strcmp(\"%s\", \"%s\")->%d\n", s2, s0, strcmp(s2, s0));
- printf("strcmp(\"%s\", \"%s\")->%d\n", s0, s2, strcmp(s0, s2));
- printf("mystrcmp(\"%s\", \"%s\")->%d\n", s2, s0, mystrcmp(s2, s0));
- printf("mystrcmp(\"%s\", \"%s\")->%d\n", s0, s2, mystrcmp(s0, s2));
- printf("mystrcmp(\"%s\", \"%s\")->%d\n", s0, s0, mystrcmp(s0, s0));
- printf("mystrcmp(\"%s\", \"%s\")->%d\n", s0, s1, mystrcmp(s0, s1));
- printf("mystrcmp(\"%s\", \"%s\")->%d\n", s1, s0, mystrcmp(s1, s0));
- printf("strcmp(\"%s\", \"%s\")->%d\n", s2, s3, strcmp(s2, s3));
- printf("mystrcmp(\"%s\", \"%s\")->%d\n", s2, s3, mystrcmp(s2, s3));
- }
$ gcc -o strcmp strcmp.c
$ ./strcmp
strcmp("ABCD", "abc")->-1
strcmp("abc", "ABCD")->1
mystrcmp("ABCD", "abc")->-1
mystrcmp("abc", "ABCD")->1
mystrcmp("abc", "abc")->0
mystrcmp("abc", "abC")->1
mystrcmp("abC", "abc")->-1
strcmp("ABCD", "ABCDE")->-1
mystrcmp("ABCD", "ABCDE")->-1
- #include <stdio.h>
- #include <string.h>
- int mystrpal( const char *s ) {
- const char *p, *q;
- for ( p = s, q = s+strlen(s)-1; p < q; ++p, --q ) {
- #if defined(DEBUG)
- printf( "%c(%d) %c(%d)\n", *p, *p, *q, *q );
- #endif
- if ( *p != *q )
- return 0;
- }
- return 1;
- }
- main( ) {
- char *s0 = "senones";
- char *s1 = "senounes";
- printf( "%s?\n", s0 );
- printf( "%d\n", mystrpal( s0 ));
- printf( "%s?\n", s1 );
- printf( "%d\n", mystrpal( s1 ));
- printf( "\"%s\"?\n", "" );
- printf( "%d\n", mystrpal( "" ));
- }
$ gcc -o strpal strpal.c
$ ./strpal
senones?
s(115) s(115)
e(101) e(101)
n(110) n(110)
1
senounes?
s(115) s(115)
e(101) e(101)
n(110) n(110)
o(111) u(117)
0
""?
1
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- static int mystrcw( const char *s ) {
- int n = 0;
- for ( ;; ) {
- while ( *s && !isalpha(*s) )
- ++s;
- if ( !*s )
- break;
- ++n;
- #if defined(DEBUG)
- printf( "%s\n", s );
- #endif
- while ( isalpha(*s) )
- ++s;
- }
- return n;
- }
- char **mystrsplit( const char *s ) {
- char **sp;
- int n = mystrcw( s );
- if ( n == 0 )
- return 0;
- sp = (char **)malloc( (n+1) * sizeof (char *));
- n = 0;
- for ( ;; ) {
- const char *p;
- char *ss;
- #if defined(DEBUG)
- const char *q;
- #endif
- while ( *s && !isalpha(*s) )
- ++s;
- if ( !*s )
- break;
- p = s;
- while ( isalpha(*s) )
- ++s;
- ss = (char *)malloc( s-p+1 );
- #if defined(DEBUG)
- q = ss;
- #endif
- sp[ n++ ] = ss;
- while ( p < s )
- *ss++ = *p++;
- *ss = '\0';
- #if defined(DEBUG)
- printf( "%s\n", q );
- #endif
- }
- sp[ n ] = 0;
- return sp;
- }
- main( ) {
- char *s0 = "\n\t$ 23@\n";
- char *s1 = "\tLe plus 'beau' de tous les tangos du monde,\n\tc'est celui que j'ai danse dans tes bras!";
- char **sp;
- int n;
- printf( "%s\n", s0 );
- n = mystrcw( s0 );
- printf( "\t= %d\n", n );
- sp = mystrsplit( s0 );
- if ( sp )
- for( ; *sp; ++sp )
- printf( "\t%s\n", *sp );
- printf( "%s\n", s1 );
- n = mystrcw( s1 );
- printf( "\t= %d\n", n );
- sp = mystrsplit( s1 );
- if ( sp )
- for( ; *sp; ++sp )
- printf( "\t%s\n", *sp );
- }
$ gcc -o strsplit strsplit.c
$ ./strsplit
$ 23@
= 0
Le plus 'beau' de tous les tangos du monde,
c'est celui que j'ai danse dans tes bras!
= 19
Le
plus
beau
de
tous
les
tangos
du
monde
c
est
celui
que
j
ai
danse
dans
tes
bras
Comments