Standard String Functions

From NutWiki

Jump to: navigation, search

Source Code

#include <dev/board.h>
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int main(void)
{
    unsigned long baud = 115200;
    char buffer[50];
    char buffer2[50];
    int l;
    char *s;
 
    NutRegisterDevice(&DEV_DEBUG, 0, 0);
 
    freopen(DEV_DEBUG_NAME, "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
 
 
    /* Copy "Hello" to the string. */
    strcpy(buffer, "Hello");
 
    /* Append " World" to buffer. */
    strcat(buffer, " World");
    printf("Text: %s\n", buffer);
 
    /* Calculate the length of the string. */
    l = strlen(buffer);
    printf("Text is %d Characters long\n", l);
 
    /* Compare two strings. */
    if (strcmp(buffer, "Hello World") == 0) {
        printf("Both strings are equal\n");
    }
 
    /* Look for the character 'l'. */
    s = strchr(buffer, 'l');
    if (s != NULL) {
        printf("'l' is at character %ld\n", s - buffer + 1);
    }
 
    /* Look for the string "World". */
    s = strstr(buffer, "World");
    if (s != NULL) {
        printf("\"World\" is at character %ld\n", s - buffer + 1);
    }
 
    /* Copy the first 5 characters to buffer2. */
    strncpy(buffer2, buffer, 5);
    buffer2[5] = '\0';
    printf("buffer2: %s\n", buffer2);
 
    /* Append 7 characters of " DeviceDevice" to buffer2. */
    strncat(buffer2, " DeviceDevice", 7);
    printf("buffer2: %s\n", buffer2);
 
    /* Compare the first 6 characters of buffer and buffer2. */
    if (strncmp(buffer, buffer2, 6) == 0) {
        printf("The first 6 characters are equal\n");
    }
 
    /* Look for the last character 'v'. */
    s = strrchr(buffer2, 'v');
    if (s != NULL) {
        printf("'v' is at character %ld\n", s - buffer2 + 1);
    }
 
    /* Create a copy of buffer2 and destroy it. */
    s = strdup(buffer2);
    printf("s: %s\n", s);
    free(s);
    s = NULL;
 
    /* Compare two strings case-insensitive. */
    if (strcasecmp(buffer2, "hello device") == 0) {
        printf("strcasecmp: equal\n");
    }
 
    /* Compare 6 character of two strings case-insensitive. */
    if (strncasecmp(buffer2, "hello world", 6) == 0) {
        printf("strncasecmp: equal\n");
    }
    /* Split buffer at all spaces. */
    s = strtok(buffer, " ");
    while (s != NULL) {
        printf("strtok: %s\n", s);
        s = strtok(NULL, " ");
    }
 
    /* Convert a string into lower case. */
    strlwr(buffer2);
    printf("buffer2: %s\n", buffer2);
 
    /* Convert a string into upper case. */
    strupr(buffer2);
    printf("buffer2: %s\n", buffer2);
 
    /* Look for the characters 'L' and 'V'. */
    s = strpbrk(buffer2, "LV");
    while (s != NULL) {
        printf("'%c' is at character %ld\n", *s, s - buffer2 + 1);
        s = strpbrk(s + 1, "LV");
    }
 
    /* Look for the character 'O' in buffer2. */
    l = strcspn(buffer2, "O");
    printf("'O' is at character %d\n", l + 1);
 
    /* Calculate how many characters at the beginning are E, H or L. */
    l = strspn(buffer2, "EHL");
    printf("%d characters at the beginneing are E, H or L\n", l);
 
 
    for (;;);
 
    return 0;
}

Output

Text: Hello World                                                               
Text is 11 Characters long                                                      
Both strings are equal                                                          
'l' is at character 3                                                           
"World" is at character 7                                                       
buffer2: Hello                                                                  
buffer2: Hello Device                                                           
The first 6 characters are equal                                                
'v' is at character 9                                                           
s: Hello Device                                                                 
strcasecmp: equal                                                               
strncasecmp: equal                                                              
strtok: Hello                                                                   
strtok: World                                                                   
buffer2: hello device                                                           
buffer2: HELLO DEVICE                                                           
'L' is at character 3                                                           
'L' is at character 4                                                           
'V' is at character 9                                                           
'O' is at character 5                                                           
4 characters at the beginneing are E, H or L                                    
Personal tools