/* Stringing.c (c) Kari Laitinen http://www.naturalprogramming.com/ 2013-11-24 File created. 2013-11-24 Last modification. */ #include #include int main() { char abcdefgh_string[] = "abcdefgh" ; char xxxxxxxx_string[] = "xxxxxxxx" ; char defgzzzz_string[ 80 ] ; strncpy( defgzzzz_string, &abcdefgh_string[ 3 ], 4 ) ; defgzzzz_string[ 4 ] = 0 ; // Terminating the string. strcat( defgzzzz_string, "zzzz" ) ; // Now defgzzzz_string points to "defgzzzz" char text_to_print[ 120 ] ; strcpy( text_to_print, " " ) ; strcat( text_to_print, abcdefgh_string ) ; strcat( text_to_print, " " ) ; strcat( text_to_print, xxxxxxxx_string ) ; strcat( text_to_print, " " ) ; strcat( text_to_print, defgzzzz_string ) ; strcat( text_to_print, " " ) ; printf( "\n" ) ; int character_index ; for ( character_index = 0 ; character_index < strlen( text_to_print ) ; character_index ++ ) { printf( "%c", text_to_print[ character_index ] ) ; } printf( "\n\n" ) ; // Next, we'll use a pointer to print the string pointed by // text_to_print. The string will be printed first in normal // order and then in reverse order. char* character_in_text ; // Here we declare a pointer // The pointer will be made to point to the first character // of the string. An array name such as text_to_print refers // to the beginning of the array character_in_text = text_to_print ; while ( *character_in_text != 0 ) { printf( " %c", *character_in_text ) ; character_in_text ++ ; } printf( "\n" ) ; // Next we'll make the pointer point to the null (zero) that // terminates the string to be printed. character_in_text = text_to_print + strlen( text_to_print ) ; while ( character_in_text > text_to_print ) { character_in_text -- ; printf( " %c", *character_in_text ) ; } printf( "\n" ) ; } /* The output of this program is the following. abcdefgh xxxxxxxx defgzzzz a b c d e f g h x x x x x x x x d e f g z z z z z z z z g f e d x x x x x x x x h g f e d c b a */