[C语言]菜鸟的一些理解-字符串

来源:互联网 发布:软件测评师考试内容 编辑:程序博客网 时间:2024/04/30 06:32
字符数组

char型数组跟int型数组的定义是一样的,唯一的是赋值不同,char型赋值要单引号‘’
还有赋值方式是一样的。

1.为数组的所以元素赋值
char c[4] = {‘g’,’o’,’o’,’d’};

2.逐个的为其赋值,
char c[4];
c[0] = ‘g’;
c[1] = ‘o’;
c[2] = ‘o’;
c[3] = ‘d’;

不指定容量,编译器会根据元素个数作为该数组容量。
char c[] = {‘g’,’o’,’o’,’d’};

int型跟char型的比较
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
 
int main(void)
{
    int a[4] = {97,98,99,100};
    char c[4] = {'g','o','o','d'};
    int i = 0;
     
    printf("以int类型输出:\n");
    for(i = 0;i < 4;++i)
        printf("%d\t",a[i]);
    printf("\n");
     
    for(i = 0;i < 4;++i)
        printf("%d\t",c[i]);
    printf("\n");
     
    printf("以char类型输出:\n");
    for(i = 0;i < 4;++i)
        printf("%c\t",a[i]);
    printf("\n");
     
    for(i = 0;i < 4;++i)
        printf("%c\t",c[i]);
    printf("\n");
    return 0;
}

输出结果:

以int类型输出:
97      98      99      100
103     111     111     100
以char类型输出:
a       b       c       d
g       o       o       d
请按任意键继续. . .

由此可以推断int型跟char型是相通的,但是只可以在1 -255之间。

假如用一个int型数组存储char型元素,可行,但是浪费存储空间。

C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
 
int main(void)
{
    int a[4] = {1,2,3,4};
    char c[4] = {'g','o','o','d'};
    //以下是计算数组所占的字节。 
    printf"sizeof(a) = %d\n",sizeof(a) );// int型所占16个字节 
    printf"sizeof(c) = %d\n",sizeof(c) );// char型所占4个字节 
    return 0;
}     

字符数组的应用(其应用跟整型数组一样)

1.引入临时数组翻转单词。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#define N 12//数组容量,该方法有利于修改 
 
int main(void)
{
    char c[N] = {'g','o','o','d',' ','m','o','r','n','i','n','g'};
    char t[N] = {'\0'};
    int i = 0;
     
    printf("翻转前:\n");
    for(i = 0;i < N;++i)
        printf("%c",c[i]);
    printf("\n");
     
    for(i = 0;i < N;++i)
        t[i] = c[N-1-i];
     
    for(i = 0;i < N;++i) 
        c[i] = t[i];
         
    printf("翻转后:\n");       
    for(i = 0;i < N;++i)
        printf("%c",t[i]);
    printf("\n");
    return 0;
}

另一种实现翻转单词。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#define N 12
 
int main(void)
{
    char c[N] = {'g','o','o','d',' ','m','o','r','n','i','n','g'};
    int i = 0,j = 0;
    char t = '\0';
     
    printf("翻转前:\n");
    for(i = 0;i < N;++i)
        printf("%c",c[i]);
    printf("\n");
     
    i = 0;
    j = N - 1;
    while(i < j)
    {
        t = c[i];
        c[i] = c[j];
        c[j] = t;
        ++i;
        --j;
    }
    printf("翻转后:\n"); 
    for(i = 0;i < N;++i)
        printf("%c",c[i]);
    printf("\n");
}

字符串

什么是字符串?
我们生活中有很多字符串,中国 北京  你的名字  地址  电话号码

字符串说白了,就是一个一维字符数组。
不过还是有区别的。
字符串是以’\0’结束.

字符串的定义---跟字符数组是一样的


字符串和字符数组的比较。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
 
int main(void)
{
    int i = 0; 
    char c[4] = {'w''o''r''k'};//定义并初始化字符数组
    char str[] = "work";//定义并初始化一个字符串
     
    printf("用字符数组输出的方式输出:\n");
     
    printf("c = ");
    for(i = 0;i < 4;++i)
        printf("%c",c[i]);
    printf("\n");
     
    printf("str = ");
    for(i = 0;i < 4;++i)
        printf("%c",str[i]);
    printf("\n用字符串输出的方式输出:\n");
     
    printf("c = %s\n",c);
    printf("str = %s\n",str);
     
    return 0;
}

运行结果

为什么用字符串输出的方式输出字符数组c 多一个方块呢?
我觉得是因为字符数组 c 没有结束标志符 \0 把别的存储空间的元素输出了,
跟数组越界一样吧。

结束符\0 的重要性
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
 
int main(void) {
    /* 定义三个字符串 */
    char a[] = "perfect\0work\0";
    char b[] = "perfect work";
    char c[4] = {'w''o''r''k'};
 
    printf("%s\n", a);               
    printf("%s\n", b);               
    printf("%s\n", c);               
 
    return 0;
}

运行结果:

为什么字符串a 不输出work  那是因为在t后面有个结束符  表示该字符串到此结束。

为什么字符串b 没有结束符,还输出正确,那是因为编译器帮我们加了。

为什么字符数组c 输出那么多不属于它的元素,那是因为他没有结束符,
如果这样改:char c[5] = {{'w', 'o', 'r', 'k','\0'};  那输出就正确了。
\0 也要占一个储存空间。只是编译器不算它。



字符串的赋值:

指定长度,(在字符串中所存储容量叫长度)
char str[5] = {“good”};  等效于 char c[5] = “good”;

不指定长度。
char str[] = “C/C++”;

字符串的应用。

字符串输入输出。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
1.
#include <stdio.h>
#define N 20
 
int main(void)
{
    char str[N] = {0};
     
    scanf("%s",str);
     
    printf("%s\n",str);//有个缺陷:空格后的字符不输出,为什么??
    return 0;
}

还好有字符串专用的输入输出函数,gets  puts
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#define N 20
 
int main(void)
{
    char str[N] = {0};
     
    printf("请输入一个字符串\n");
    gets(str);
     
    puts(str); 
    return 0;
}

字符串的应用。

1.统计单词数
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    int i = 0;
    int len = 0;//字符串长度 
    int count = 0;//计数 
    char str[N] = {0};
     
    printf("请输出一个字符串:\n");
    gets(str);
     
    len = strlen(str);//计算str的长度
     
    for(i = 0;i < len;++i)
    {
        if(str[i] != ' ')
        {
            ++count;
            while(str[i] != ' ' && str[i] != '\0')
            ++i;
        }
    }
    printf("单词个数:%d\n",count);
    return 0;
}

2.颠倒单词顺序。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <string.h>
#define N 256
int main(void)
{
    int head = 0;//前置索引 
    int end = 0;//后置索引 
    int len = 0;//长度 
    int t = 0;//临时变量 
    int i = 0;//索引 
    char str[N];//定义一个字符串 
     
    printf("请输入一个字符串:");
    gets(str);
  
     len = strlen(str);//得到字符串的长度 
      
     end = len - 1;//len -1  
     head = 0;
      
     while(head < end)//实现字符串翻转 
     {
         t = str[head];
         str[head] = str[end];
         str[end] = t;
         ++head;
         --end;
     }
     printf("翻转后的字符串:\n"); 
     puts(str);
      
     for(i = 0;str[i] != '\0';++i)
     {
         if(' ' != str[i])//
         {
             head = i;//如果不等于空格,把i赋值给head  
            while(str[i] != ' ' && str[i] != '\0')/*遍历字符串中的元素,找到空格结束循环。*/ 
                ++i;
            end = i - 1;
            while(head < end)
            {
                t = str[head];
                str[head] = str[end];
                str[end] = t;
                ++head;
                --end;
            }
         }
     }
     printf("颠倒后:\n"); 
     puts(str);
    return 0;
}

掠过多余的空格。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
#include<string.h>
#define N 256
int main(void)
{
    char str[N];
    int i = 0, j = 0,len = 0,t = 0;
     
    printf("请输入str:");
    gets(str); 
    len = strlen(str);
     
    for(i=0,j=0;i < len;++i,++j)
    {
        str[j] = str[i];
        if(' ' == str[i])
        {
            ++i;
            while(' ' == str[i])
                ++i;
            --i;
        }
    }
    str[j] = '\0';
    printf("%s\n",str);
    puts(str);
    return 0;
}

字符串处理
要用到的头文件:#include <stdio.h>
复制:strcpy

strcpy(s1,s2);//将字符串s2复制到 字符串s1 中、
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    char s1[N] = {0};
    char s2[N] = {0};
     
    printf("请输入一个字符串:");
    gets(s2);
     
    strcpy(s1,s2);
     
    printf("s1 = %s\n",s1);
    printf("s2 = %s\n",s2);
     
    return 0;
}

不用strcpy函数实现复制
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    int i = 0;
    int len = 0;
    char s1[N];
    char s2[N];   
 
    printf("请输入一个字符串s2:\n");
    gets(s2);
    len  = strlen(s2);
             
    for(i = 0;i < len;++i)
        s1[i] = s2[i];
     
    printf("s1 = %s\n",s1);
    printf("S2 = %s\n",s2);
     
    return 0;
}

拼接:strcat(s1,s2);

strcat(s1,s2);//将字符串s2 拼接到 字符串s1 后面
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    char s1[N] = "include ";
    char s2[N] = "<string.h>";
     
    strcat(s1,s2);
     
    printf("s1 = %s\n",s1);
    printf("s2 = %s\n",s2);
    return 0;
}

不用strcat函数实现拼接
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    int len = 0,i;
    int end = 0;
    char s1[N] = "#include ";
    char s2[N] = "<stdio.h>";
     
    len = strlen(s1);
    end = len;
     
    for(i = 0;s2[i] != '\0';++i,++end)
        s1[end] = s2[i];
    s1[end++] = '\0';
     
    printf("s1 = %s\n",s1);
    printf("s2 = %s\n",s2);
     
    return 0;
}

比较:strcmp(s1,s2);
strcmp(s1,s2);//比较s1 和s2  如果字符串s1大于字符串s2返回1,等于返回0 字符串s1 小于 字符串 s2 返回 -1 

C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    char s1[N];
    char s2[N];
    int i = 0;
     
    printf("请输入字符串s1:");
    gets(s1);
     
    printf("请输入字符串s2:");
    gets(s2);
     
    i = strcmp(s1,s2);
    switch(i)
    {
        case -1:
            printf("s1 < s2\n");
            break;
        case 0:
            printf("相等\n");
            break;
        case 1:
            printf("s1 > s2\n");
  break;
        default:
            printf("异常!\n");
    }
    return 0;
}

不用strcmp函数实现比较
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    char s1[N];
    char s2[N];
    int i = 0,t = 0;
     
    printf("请输入字符串s1:");
    gets(s1);
     
    printf("请输入字符串s2:");
    gets(s2);
     
    if(s1[i] == s2[i])//判断是否相等 
    {
        while(s1[i] != '\0' && s1[i] == s2[i])//循环判断若等于 ++i 
        {
            ++i;
            if(s1[i] == '\0')//表示字符串s1 以遍历结束 
                t = 0;
        }
    }
     
    else if(s1[i] < s2[i])
        t = -1;
    else if(s1[i] > s2[i])
        t = 1;
    else 
        printf("异常!\n");
          
    switch(t)
    {
        case -1:
            printf("s1 < s2\n");
            break;
        case 0:
            printf("相等\n");
            break;
        case 1:
            printf("s1 > s2\n");
            break;
        default:
            printf("异常!\n");
    }
    return 0;
}

这个有缺陷:字符串s2元素多出字符串s1的元素不比较。
望高手帮我改改。

转换:strupr(s1);
strupr(s1);//将字符串s1中的所以小写字母转换成大写字母
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    char s1[N];
     
    printf("请输入一个字符串:");
    gets(s1);
     
    printf("转换前:\n");
    puts(s1);
     
    strupr(s1);
     
    printf("转换后:\n");
    puts(s1);
     
    return 0;
}

不用strupr函数实现转换
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#define N 100
 
int main(void)
{
    int i = 0;
    char s1[N];
     
    printf("请输入一个字符串:\n");
    gets(s1);
     
    for(i = 0;s1[i] != '\0';++i)
        if(s1[i] >= 'a' && s1[i] <= 'z')
            s1[i] -= 32;
             
    printf("转换后:\n");
    puts(s1);
     
    return 0;
}

转换:strlwr(s1);
strlwr(s1);//将字符串s1中的所以大写字母转换成小写字母
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>
#define N 100
 
int main(void)
{
    char s1[N];
     
    printf("请输入一个字符串:");
    gets(s1);
     
    printf("转换前:\n");
    puts(s1);
     
    strlwr(s1);
     
    printf("转换后:\n");
    puts(s1);
     
    return 0;
}

不用strlwr函数实现转换
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#define N 100
 
int main(void)
{
    int i = 0;
    char s1[N];
     
    printf("请输入一个字符串:\n");
    gets(s1);
     
    for(i = 0;s1[i] != '\0';++i)
        if(s1[i] >= 'A' && s1[i] <= 'Z')
            s1[i] += 32;
             
    printf("转换后:\n");
    puts(s1);
     
    return 0;
}


写的不怎么样   勿喷!!
没有理解到的或理解错误的,尽管批评我,你的批评,我的动力。
原创粉丝点击