Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7540639
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-05-08 16:22:54

1、找出字符串中所有英文字母

#include

#include

#include

 

int main()

{

    char str[100];        /* 定义字符串数组 */

    int i;

   

    printf("输入一串字符:");

    scanf("%s",str);

   

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

    {

        if(isalpha(str[i]))          /* 测试是否为字母 */

            printf("%c",str[i]);

    }

    printf("\n");

    return 0;

}

 

2、是否为字母

#include

#include

 

int main()

{

    char c;

    printf("输入:");

    while((c = getchar()) != EOF)           /* 从键盘输入 */

    {      

        if(isalpha(c))                        /* 测试 */

            printf("\n");

        else

            printf("不是\n");

        getchar();    

    }

 

    return 0;

}

 

3、测试是否为数字

#include

#include

 

int main()

{

    char c;

    printf("输入:");

    while((c = getchar()) != EOF)           /* 从键盘输入 */

    {      

        if(isdigit(c))                        /* 测试 */

            printf("\n");

        else

            printf("不是\n");

        getchar();

        printf("\n输入:");

    }

 

    return 0;

}

 

4、字符串初始化

#include

#include

 

int main()

{

    char str[20];

    memset(str,'H',10);       /* 初始化字符串数组 */

    puts(str);

}

 

 

#include

#include

 

int main()

{

    int str[10], i;

    bzero(str, sizeof(str));    /* 数组清0 */

    for(i = 0; i < 10; i++)

        printf("%d ",str[i]);

    printf("\n");

}

 

 

 

5、字符串复制

#include

#include

 

int main()

{

    char str[10], str1[]={"Lzy"};

    strcpy(str,str1);             /* 把字符串str1复制给str */

    printf("%s\n",str);

}

 

6、字符串比较。模拟用户登陆,当用户用户名与密码输入正确,提示登陆成功,否则重新输入

 

#include

#include

 

int main()

{

    char usrname[] = {"Lzy"};

    char passwad[] = {"123456"};

    char str[10];

   

    while(1)

    {

        printf("输入用户名:");

        scanf("%s",str);

        if(strcmp(str,usrname) == 0)

        {

            printf("输入密码:");

            scanf("%s",str);

            if(!strcmp(str,passwad))

            {

                printf("登陆成功!\n");

                return 0;

            }      

        }

        else

            printf("请重试\n");

    }

}

 

 

7、字符\字符串查找。

7.1在字符串中查找指定的字符

#include

#include

 

int main()

{  

    char str[] = {"Hello World"};

    char *p;  

    p = index(str,'W');          /* 查找字符 */

    if(p != NULL)

        printf("找到的字符%c\n",*p);

    return 0;

}

 

7.2在字符串中查找指定的字符串

#include

#include

 

int main()

{  

    char str[] = {"Hello World"};  

    char s[] = "Wo";

    char *p;  

    p = strstr(str,s);           /* 查找字符串 */

    if(p != NULL)

        printf("找到的字串%s\n",p); 

    return 0;

}

 

8、字符串连接\分割

8.1字符串连接

#include

#include

 

int main()

{  

    char str[20] = "I lvoe ";

    strcat(str, "this girl");           /* this girl放到str最后一个字符的后面 */

    printf("%s\n",str); 

    return 0;

}

 

8.2字符串分割

#include

#include

 

int main()

{  

    char str[] = "I lvoe this girl";

    char *p;

    p = strtok(str, " ");                /* 以空格对字符串进行分割 */

    while(p != NULL)

    {

        printf("%s\n",p);             /* 输入分割的字符串 */

        p = strtok(NULL, " ");           /* 继续分割 */

    }  

   

    return 0;

}

一小时——部分:Linux C

阅读(2035) | 评论(0) | 转发(3) |
0

上一篇:C_函数

下一篇:C_数据转换

给主人留下些什么吧!~~