getch() getchar()

来源:互联网 发布:大恒软件 编辑:程序博客网 时间:2024/05/22 10:42
#include <stdio.h>          // for getchar()#include <string.h>#include <stdlib.h>         // for system("pause")#include <conio.h>          // for getch()int main(){    char sz[17] = {0};    char *ptr = NULL, c = 'r';    strcpy(sz, "This is a string");    ptr = strchr(sz, c);    if(ptr)    {        printf("The character %c is at position: %s\n", c, ptr);        printf("Before change:%s\n", sz);        *ptr = '\0';        printf("After change:%s\n", sz);    }    else        printf("The character was not found\n");    printf("\n\n");    char sz1[20] = {0};    char *ptr1 = NULL, c1 = 'r';    strcpy(sz1, "There are two rings");    ptr1 = strrchr(sz1, c1);    if(ptr1)        printf("The character %c is at position: %s\n", c1, ptr1);    else        printf("The character was not found\n");    // 不等待用户按回车,只要用户按一个键,getch()就立即返回,    // 返回ASCII码,出错返回-1,输入的字符不会回显在屏幕上    // 非缓冲输入函数    char nch1 = (char)getch();    printf("%c\n", nch1);    // 输入的字符回显在屏幕上,缓冲输入函数    char nch2 = (char)getchar();    printf("%c\n", nch2);    system("pause");    return 0;}
原创粉丝点击