strdup tolower toupper 实例



源码:

robin@ubuntu:~/workspace/c_workspace$ cat lowerToUp.c

/****************************************************
 *   strdup() toupper() tolower() 实例
 *   Robin
 * *************************************************/

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int main(){
    char *str = "Hello Robin!!!!";
    char *s, *each;

    /* strdup 调用了malloc函数在heap上为s分配内存,并赋值str*/
    s = strdup(str);
    each = s;
    while(*each != '\0'){
        if(*each <= 90 && *each >= 65)
            *each = tolower(*each);
        else if(*each <= 122 && *each >=97)
            *each = toupper(*each);
        else 
            *each = *each;
        each++;
    }
    printf("str = %s\ns = %s\n", str, s);
    return 0;
}

Makefile

robin@ubuntu:~/workspace/c_workspace$ cat Makefile
SRC=lowerToUp.c
OBJ=lowerToUp
CC=gcc

all:
	${CC} ${SRC} -o ${OBJ}

clean:
	rm -rf $(OBJ)

你可能感兴趣的:(strdup tolower toupper 实例)