75

Functions on character strings

  1. int mystrlen1( const char *s ) {
  2.     int i;
  3.  
  4.     for ( i = 0; s[ i ]; i++ )
  5.         ;
  6.     return i;
  7. }
  8.  
  9. int mystrlen2( const char *s ) {
  10.     int i;
  11.  
  12.     for ( i = 0; *s; i++ )
  13.         ++s;
  14.     return i;
  15. }
  16.  
  17. int mystrlen3( const char *s ) {
  18.     const char *p = s;
  19.  
  20.     while ( *s )
  21.         ++s;
  22.  
  23.     return s-p;
  24. }
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. main( ) {
  30.     char *s0 = "Hello, my dear!";
  31.     char *s1 = "";
  32.  
  33.     printf( "\"%s\"->15=%d\n", s0, strlen( s0 ));
  34.     printf( "\"%s\"->15=%d\n", s0, mystrlen1( s0 ));
  35.     printf( "\"%s\"->15=%d\n", s0, mystrlen2( s0 ));
  36.     printf( "\"%s\"->15=%d\n", s0, mystrlen3( s0 ));
  37.  
  38.     printf( "\"%s\"->0=%d\n", s0, strlen( s1 ));
  39.     printf( "\"%s\"->0=%d\n", s0, mystrlen1( s1 ));
  40.     printf( "\"%s\"->0=%d\n", s0, mystrlen2( s1 ));
  41.     printf( "\"%s\"->0=%d\n", s0, mystrlen3( s1 ));
  42. }
$ 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
  1. char *mystrcpy( char *s, const char *s0 ) {
  2.     char *p = s;
  3.  
  4.     while ( *s++ = *s0++ )
  5.         ;
  6.     return p;
  7. }
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13. main( ) {
  14.     char *s0 = "Hello, my copy cat!";
  15.     char *s1 = (char *)malloc( strlen( s0 ) + 1);
  16.  
  17.     printf( "s0=%s\n", s0);
  18.     printf( "strlen(s0)=%d\n", strlen( s0 ));
  19.     printf( "mystrcpy(s1, s0)->0x%x=0x%x\n", (unsigned)s1, (unsigned)mystrcpy( s1, s0 ));
  20.     printf( "s1=%s\n", s1);
  21.     printf( "strlen(s1)=%d\n", strlen( s1 ));;
  22.     printf( "s0=%s\n", s0);
  23.     printf( "strlen(s0)=%d\n", strlen( s0 ));
  24. }
$ 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
  1. #include <string.h>
  2.  
  3. char *mystrcat(char *s, const char *s0) {
  4.     char *p = s;
  5.  
  6.     strcpy(s+strlen(s), s0);
  7.  
  8.     return p;
  9. }
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. main() {
  15.     char *s0 = "Hello";
  16.     char *s1 = "my copycat!";
  17.     char *sep = ", ";
  18.     char *s = (char *)malloc(strlen(s0) + strlen(sep) + strlen(s1) + 1);
  19.  
  20.     printf("s0+sep+s1=%s%s%s\n", s0, sep, s1);
  21.     strcpy(s, s0);
  22.     mystrcat(s, sep);
  23.     mystrcat(s, s1);
  24.     printf("s0+sep+s1=%s\n", s);
  25. }
$ gcc -o strcat strcat.c
$ ./strcat
s0+sep+s1=Hello, my copycat!
s0+sep+s1=Hello, my copycat!
  1. #include <string.h>
  2.  
  3. int mystrcmp(const char *s1, const char *s2) {
  4.     while ( *s1 && *s2) {
  5.         if ( *s1 < *s2)
  6.             return -1;
  7.         if ( *s1 > *s2)
  8.             return 1;
  9.         else
  10.             ++s1, ++s2;
  11.     }
  12.  
  13.     /* add the following lines after discussion */
  14.     if (*s1 == '\0' && *s2 == '\0')
  15.         return 0;
  16.     return (*s1 == '\0') ? -1 : 1;
  17. }
  18.  
  19. #include <stdio.h>
  20.  
  21. main() {
  22.     char *s0 = "abc";
  23.     char *s1 = "abC";
  24.     char *s2 = "ABCD";
  25.     char *s3 = "ABCDE";
  26.  
  27.     printf("strcmp(\"%s\", \"%s\")->%d\n", s2, s0, strcmp(s2, s0));
  28.     printf("strcmp(\"%s\", \"%s\")->%d\n", s0, s2, strcmp(s0, s2));
  29.     printf("mystrcmp(\"%s\", \"%s\")->%d\n", s2, s0, mystrcmp(s2, s0));
  30.     printf("mystrcmp(\"%s\", \"%s\")->%d\n", s0, s2, mystrcmp(s0, s2));
  31.     printf("mystrcmp(\"%s\", \"%s\")->%d\n", s0, s0, mystrcmp(s0, s0));
  32.     printf("mystrcmp(\"%s\", \"%s\")->%d\n", s0, s1, mystrcmp(s0, s1));
  33.     printf("mystrcmp(\"%s\", \"%s\")->%d\n", s1, s0, mystrcmp(s1, s0));
  34.     printf("strcmp(\"%s\", \"%s\")->%d\n", s2, s3, strcmp(s2, s3));
  35.     printf("mystrcmp(\"%s\", \"%s\")->%d\n", s2, s3, mystrcmp(s2, s3));
  36. }
$ 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
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int mystrpal( const char *s ) {
  5.     const char *p, *q;
  6.  
  7.     for ( p = s, q = s+strlen(s)-1; p < q; ++p, --q ) {
  8. #if defined(DEBUG)
  9.         printf( "%c(%d) %c(%d)\n", *p, *p, *q, *q );
  10. #endif
  11.         if ( *p != *q )
  12.             return 0;
  13.     }
  14.     return 1;
  15. }
  16.  
  17. main( ) {
  18.     char *s0 = "senones";
  19.     char *s1 = "senounes";
  20.  
  21.     printf( "%s?\n", s0 );
  22.     printf( "%d\n", mystrpal( s0 ));
  23.     printf( "%s?\n", s1 );
  24.     printf( "%d\n", mystrpal( s1 ));
  25.     printf( "\"%s\"?\n", "" );
  26.     printf( "%d\n", mystrpal( "" ));
  27. }
$ 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
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4.  
  5. static int mystrcw( const char *s ) {
  6.     int n = 0;
  7.  
  8.     for ( ;; ) {
  9.         while ( *s && !isalpha(*s) )
  10.             ++s;
  11.         if ( !*s )
  12.             break;
  13.         ++n;
  14. #if defined(DEBUG)
  15.         printf( "%s\n", s );
  16. #endif
  17.         while ( isalpha(*s) )
  18.             ++s;
  19.     }
  20.     return n;
  21. }
  22.  
  23. char **mystrsplit( const char *s ) {
  24.     char **sp;
  25.  
  26.     int n = mystrcw( s );
  27.  
  28.     if ( n == 0 )
  29.         return 0;
  30.  
  31.     sp = (char **)malloc( (n+1) * sizeof (char *));
  32.     n = 0;
  33.  
  34.     for ( ;; ) {
  35.         const char *p;
  36.         char *ss;
  37. #if defined(DEBUG)
  38.         const char *q;
  39. #endif
  40.         while ( *s && !isalpha(*s) )
  41.             ++s;
  42.         if ( !*s )
  43.             break;
  44.  
  45.         p = s;
  46.  
  47.         while ( isalpha(*s) )
  48.             ++s;
  49.  
  50.         ss = (char *)malloc( s-p+1 );
  51. #if defined(DEBUG)
  52.         q = ss;
  53. #endif
  54.         sp[ n++ ] = ss;
  55.         while ( p < s )
  56.             *ss++ = *p++;
  57.         *ss = '\0';
  58. #if defined(DEBUG)
  59.         printf( "%s\n", q );
  60. #endif
  61.     }
  62.  
  63.     sp[ n ] = 0;
  64.  
  65.     return sp;
  66. }
  67.  
  68. main( ) {
  69.     char *s0 = "\n\t$ 23@\n";
  70.     char *s1 = "\tLe plus 'beau' de tous les tangos du monde,\n\tc'est celui que j'ai danse dans tes bras!";
  71.  
  72.     char **sp;
  73.     int n;
  74.  
  75.     printf( "%s\n", s0 );
  76.     n = mystrcw( s0 );
  77.     printf( "\t= %d\n", n );
  78.     sp = mystrsplit( s0 );
  79.     if ( sp )
  80.         for( ; *sp; ++sp )
  81.             printf( "\t%s\n", *sp );
  82.  
  83.     printf( "%s\n", s1 );
  84.     n = mystrcw( s1 );
  85.     printf( "\t= %d\n", n );
  86.     sp = mystrsplit( s1 );
  87.     if ( sp )
  88.         for( ; *sp; ++sp )
  89.             printf( "\t%s\n", *sp );
  90. }
$ 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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].