一、字符串处理
1、字符串表示和处理方式
在C语言中没有专门的字符串类型,字符串可以用三种方式表示和处理: (1)字面值方式:“xsteach”。不能修改,但是可以被赋值给变量,并在变量中修改。字面值方式的字符串后面自动追加’\0’结束符。字面值可以拼接: " abcdg" ==> “erghjj” (2)用字符数组:
void main( )
{
char string[]="How are you?" ;
printf("%s\n" , string) ;
}
(3)用字符指针: ①指向字面值字符串
char *str="I love China!";
char *str;
str="I love china!";
②指向字符数组字符串。同样有效字符串必须以’\0’结尾。
char *a,str[]={"hello world!"};
a=str;
字符指针变量中存放的仅是一个地址(字符串第1个字符的地址),决不是将字符串放到字符指针变量中。
二、字符数组
1、定义
字符数组由若干个元素组成,每个元素中放一个字符。 字符数组名是常量绝不允许改变,但字符数组元素值可改变。
2、初始化
char str[14]={"I love China!"};
3、赋值
字符数组只能对其各个元素逐个赋值,不能对字符数组整体赋值。
char str[14];
strcpy(str,"I love china!");
4、字符数组地址
定义了一个字符数组,在编译时为它分配内存单元,它有确定的地址。
char str [10];
scanf ("%s",str);
5、字符数组应用
例1:
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="this is Love!";
char a1[]="ABC";
char a2[]={"ABC"};
char b1[]="'A','B','C','\0'";
char b2[]={'A','B','C','\0'};
char b3[]={"'A','B','C','\0'"};
printf("%s\n",str);
str[2]='g';
*(str+2)='2';
str[6]=0;
printf("%s\n",str);
str[6]='0';
printf("%s\n",str);
printf("a1=%d,a2=%d\n",sizeof(a1),sizeof(a2));
printf("b1=%d,b2=%d,b3=%d\n",sizeof(b1),sizeof(b2),sizeof(b3));
return 0;
}
例2:
#include <stdio.h>
#include <string.h>
int main()
{
char a[]="I am a student.",b[20];
int i;
for(i=0;*(a+i)!='\0';i++)
{
*(b+i)=*(a+i);
}
*(b+i)='\0';
printf("字符串a=%s\n",a);
printf("字符串b=");
for(i=0;b[i]!='\0';i++)
{
printf("%c",b[i]);
}
printf("\n");
return 0;
}
例3:
#include <stdio.h>
#include <string.h>
void copy_string(char from[],char to[])
{
int i=0;
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
int main()
{
char a[]="I am a teacher.";
char b[]="you are a student. ";
printf("字符串a=%s\n字符串b=%s\n",a,b);
printf("字符串a拷贝到b:\n ");
copy_string(a,b);
printf("\n字符串a=%s\n字符串b=%s\n",a,b);
return 0;
}
例4:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int i=0,j=0;
char src[100]="hello,world";
char dest[100]="i am fine";
printf("src=%s\n",src);
printf("dest1=%s\n",dest);
while(dest[i]!='\0')
{
i++;
}
while(src[j]!='\0')
{
dest[i]=src[j];
i++;
j++;
}
printf("dest2=%s\n",dest);
return 0;
}
三、指向字符数组的指针变量
1、概念
(1)C语言中的字符串,若没存放到数组中,就放在常量区中,常量区中的数据不能改变。 (2)如果要修改字符串内容,就一定要把它放在数组中。 (3)新定义的指针变量,无具体的值.解决办法是,把一个已定义的字符数组的地址赋给这个指针变量。字符指针变量的值是可以改变的。
2、使用字符指针存储和访问字符串
char* pStr;
char str[10]=“hello”;
pStr=str;
3、字符指针作函数的参数
将一个字符串从一个函数传递到另一个函数,可以用地址传递的办法,即用字符数组名作为参数或用指向字符串的指针变量作为参数。在被调用的函数中可以改变字符串的内容,在主调函数中可以得到改变后的字符串。
4、指向字符数组的指针变量应用
例1:
#include <stdio.h>
#include <string.h>
int main()
{
int i=0,j=0;
char buf[10]="hello";
char *p=buf;
for(i=0;i<strlen(buf);i++)
{
printf("buf[i]=%c\n",buf[i]);
}
return 0;
}
例2:
#include <stdio.h>
#include <string.h>
int main()
{
char a[]="I am a student.",b[20],*p1,*p2;
p1=a;p2=b;
for(;*p1 !='\0';p1++,p2++)
{
*p2=*p1;
}
*p2='\0';
printf("字符串a=%s\n",a);
printf("字符串a=%s\n",b);
return 0;
}
例3:
#include <stdio.h>
#include <string.h>
void copy_string(char from[],char to[])
{
int i=0;
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
int main()
{
char a[]="I am a teacher.";
char b[]="you are a student.";
char *from=a,*to=b;
printf("字符串a=%s\n字符串b=%s\n",a,b);
printf("字符串a拷贝到b:\n ");
copy_string(from,to);
printf("\n字符串a=%s\n字符串b=%s\n",a,b);
return 0;
}
例4:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void copy_string (char *from,char *to)
{
for(;*from !='\0';from++,to++)
{
*to=*from;
}
*to ='\0';
}
int main()
{
char *a="I am a teacher.";
char b[]="you are a student. ";
char *p=b;
printf("字符串a=%s\n字符串b=%s\n",a,b);
printf("字符串a拷贝到b:\n ");
copy_string(a,p);
printf("\n字符串a=%s\n字符串b=%s\n",a,b);
return 0;
}
例5:
#include <stdio.h>
#include <string.h>
int main()
{
char acbuf[]={"hello"};
char *p;
int i=0,len=0;
p=acbuf;
len=strlen(acbuf);
for(i=len-1;i<=len && i>=0;i--)
{
printf("%c",*(p+i));
}
printf("\n");
return 0;
}
四、指向字符串常量的字符指针
1、概念
(1)可以声明字符指针直接指向字符串常量,例如char* pStr=“Welcome”;字符指针pStr将指向字符串常量“Welcome”,“Welcome”为字符串常量,因此其值不能修改,例如代码pStr[0]=‘h’;将引发一个错误。 (2)C语言对字符串常量按字符数组处理,在内存中开辟了一个字符数组用来存放该字符串变量,但这个字符数组没有名字,不能通过数组名引用,只能通过指针变量来引用。对字符指针变量初始化,实际指向字符串第一个元素的地址。 (3) C语言中的字符串,如果没存放到数组中,就是放在常量区中,而常量区中的数据是不能改变的。换言之如果要修改字符串内容,就一定要把它放在数组中。
2、指向字符串常量的字符指针应用
例1:
#include "stdio.h"
#include "string.h"
void main( )
{
int i=0;
char *p="china";
char *pt="ABCDEFGHIJK";
printf("输出单个字符:");
printf("p[2]=%c\n",p[2]);
printf("整串输出:");
printf("%s\n",p);
printf("指针法逐个字符输出:");
while (*p!='\0')
{
printf("%c",*p++);
}
printf("\n下标法输出字符串:");
for(i;pt[i]!='\0';i++)
{
printf("%c",pt[i]);
}
printf("\npt[3]=");
putchar(pt[3]);
printf("\n");
printf("pt=%s\n",pt);
pt=pt+2;
printf("pt+2=%s\n",pt);
return;
}
例2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Mycopy(char *des,char *src)
{
while(*src!='\0')
{
*des=*src;
des++;
src++;
}
}
int main(int argc,char const *argv[])
{
char *str1="this is a test";
char *str2=(char *)malloc(strlen(str1));
Mycopy(str2,str1);
printf("str2=%s\n",str2);
return 0;
}
例3:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void myStrCpy(char **des, char *src)
{
int i = 0;
if(*des == NULL)
{
printf("malloc\n");
*des = (char *)malloc(strlen(src)*sizeof(char));
}
while(*src != '\0')
{
**des = *src;
(*des)++;
src++;
i++;
}
*des=*des-i;
}
int main(int argc, char const *argv[])
{
char *str1 = "this is a test";
char *str2 = NULL;
printf("address of \"this is a test\" is %p\n", str1);
printf("address of str2 is %p\n", &str2);
myStrCpy(&str2,str1);
printf("str2: %s\n", str2);
return 0;
}
例4:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Mycopy(char *des,char *src,int m)
{
while(*src!='\0' && m>0)
{
src++;
m--;
}
if(m==0)
{
while(*src!='\0')
{
*des++=*src++;
}
}
else
{
printf("你要开始拷贝的位置,超过了源字符串\n");
return -1;
}
return 0;
}
int main(int argc,char const *argv[])
{
char *str1="this is a test";
char *str2;
int ret;
str2=(char*)malloc(sizeof(char)*strlen(str1));
ret=Mycopy(str2,str1,13);
if(ret==-1)
{
printf("拷贝出错,退出\n");
exit(-1);
}
printf("str1:%s\n",str1);
printf("str2:%s\n",str2);
return 0;
}
例5:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
void mycopy(char *dest,char *src,int len)
{
int i=0;
char *s=src;
while(*s!='\0')
{
*s++;
i++;
if(i==10)
{
printf("*s1=%c\n",*s);
}
}
printf("i=%d\n",i);
printf("len=%d\n",len);
while(len--!=0)
{
*dest++=*(--s);
}
}
int main()
{
char *src="hello,world";
char *dest=(char *)malloc(strlen(src));
int len=strlen(src);
mycopy(dest,src,len);
printf("dest2=%s\n",dest);
return 0;
}
例6:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
void connect(char *dest,char *string,int len)
{
int i=0,j=0;
while(dest[i]!='\0')
{
i++;
}
printf("i=%d\n",i);
while(string[j] !='\0')
{
dest[i]=string[j];
i++;
j++;
}
dest[i]='\0';
}
int main()
{
char src[20]="hello,world";
char *str="i am fine";
int len=sizeof(str);
printf("src1=%s\n",src);
connect(src,str,len);
printf("src2=%s\n",src);
return 0;
}
例7:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
void connect(char *dest,char *string,int len)
{
int i=0,j=0;
while(*dest!='\0')
{
*dest++;
i++;
}
printf("len=%d\n",len);
dest--;
while(len--!=0)
{
*dest=*string;
dest++;
string++;
}
*dest='\0';
}
int main()
{
char src[100]="hello,world";
char *str="i am fine";
int len=strlen(str);
printf("src1=%s\n",src);
connect(src,str,len);
printf("src2=%s\n",src);
return 0;
}
编辑 2020-09-13 02:48 首次编辑 增改 2021-07-15 23:26 内容结构优化
注:本文旨于作为自己的学习笔记,不作他用。
|