------------------------------------------------------------------------------
MC logo
C Strings
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[C Strings] [More C Strings] [Plain C Arg Lister] [Fancy Plain C Arg Lister (With Pointers)] [Simple String Sort]
str1.c
/*
 * String games. 
 *
 * C strings are arrays of characters, terminated with a zero byte (null
 * character).
 */
#include <stdio.h>

/* Print a string (printf %s can do this, but let's show how.)   This can
   be made shorter using pointer arithmatic. */
void prstr(char *str)
{
        int i;

        for(i = 0; str[i] != '\0'; ++i)
                putchar(str[i]);
}

/* Have fun with strings. */
int main()
{
        char s1[25];
        char s2[] = "Oh boy, a string!";
        char *cp;

        s1[0] = 'T';
        s1[1] = 'o';
        s1[2] = 'm';
        s1[3] = '\0';
        prstr(s1);
        printf(" %s\n\n", s1);

        /* Print the string (and parts thereof). */
        prstr(s2); putchar('\n');
        printf("%s\n", s2 + 4);
        cp = s2 + 11;
        s2[5] = '\0';
        printf("[%s] [%s]\n", s2, cp);
}