简体   繁体   中英

C - Printing first letter of a string using pointers

Im currently working on a project for college and i have the following function, which receives a line (string) and divides it into words:

static const char separators[] = {' ','\t',',',';','.','?','!','"','\n',':','\0'}; 

void split(char *line){
    char *token = strtok(line, separators);
    while(token!=NULL) {
        strtolower(token);
        printf("%s \n", token);
        /* rest of code belongs here */
        token = strtok(NULL, separators);
    }
}

For testing purposes, I wanted to print the first letter of the string token , however it prints the whole string and whenever i use other methods (*token, token[0]) it creates an error stating that %s expects type *char and not int .

How can i print only the first letter of the string, for future implementation in my code?

Very simply:

printf("%c\n", *token);

or

printf("%c\n", token[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM