数据结构学习第1篇 - 字符串的处理

2017年(大一下)学数据结构写的代码,学数据结构可以说是受益匪浅,这几天找了一下代码,放到csdn分享一下,也记录一下我的代码


C语言复习1            

1.实验类别:基础

2.实验目的:

(1)掌握C语言编写程序的方法

(2)掌握指针的使用方法。

3.实验主要内容:

请设计一程序,实现如下功能:接收用户输入一组字符串,对于字符串中的每一个单词(单词之间以空格分割),首字母转变成大写字母,其余字母转变成小写字母,其余符号不便,并把处理前后的结果在屏幕上打印出来。

要求:(1)使用指针方法处理字符串。

     (2)不能使用C语言字符串处理库函数。

4.实验类型:综合、设计

5.实验要求:必修

6.主要仪器:计算机

扫描二维码关注公众号,回复: 5784118 查看本文章

# include <stdio.h>
# include <malloc.h>
# include <string.h>

//功能菜单
void ChoiseMenu(void)
{
    printf("Option:\n");
    printf("\t1.Convert\n");
    printf("\t2.Quit\n");
    
}

//转化函数
void Convert(char *s)
{
    int i;

    //转化
    for(i=0;*(s+i)!='\0';i++)
    {
        //对字符数组首单元进行处理
        if(*s>='a'&&*s<='z')
        {
            *s=*s-32;
        }
        if(i>0)
        {
            //对每个单词首字母转化为大写
            if(((*(s+i-1)>'Z'&&*(s+i-1)<'a')||*(s+i-1)>'z'||*(s+i-1)<'A')&&(*(s+i)>='a'&&*(s+i)<='z'))
            {
                *(s+i)=*(s+i)-32;
            }
            
            //对每个单词的非首字母的大写字母转为小写
            if(((*(s+i-1)>='a'&&*(s+i-1)<='z')||(*(s+i-1)>='A'&&*(s+i-1)<='Z'))&&(*(s+i)>='A'&&*(s+i)<='Z'))
            {
                *(s+i)=*(s+i)+32;
            }
        }
    }  
    printf("\tResult:");//输出
    puts(s);
}

int main()
{
    
    char *p=(char*)malloc(256);
    int op;
    ChoiseMenu();
    while(1)
    {
        printf("Please input your option:");
        scanf("%d",&op);//选择
        switch(op)
        {
            case 1:
                printf("Please input your sentence:");//交互
                getchar();
                gets(p);//输入
                
                Convert(p);
                break;
            case 2:
                return 0;
            default:
                printf("\tError!\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guanshanyue96/article/details/89005952
今日推荐