认识字符串
[字符串]一串字符,最有一个致富思“、0”,该字符一般称为“尾零”,其ascii的值为0,是一个不可见的字符,标识一个字符串的结束。
1、isaplpha(判断字母)
判断字符chr是否为英文字母,是字母返回1,不是字母返回0;
原型:⬇️
include
include
int myIsalpha(int c);
int main ()
{
char chr = '\0';
scanf("%c",&chr);
if(myIsalpha(chr))
{
printf("%c 是字母\n",chr);
}
else
{
printf("%c不是字母\n",chr);
}
return 0;
}
int myIsalpha(int c)
{
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
return 1;
}
return 0;
}
isnumber() 判断是否是数字字符
如果是数字字符返回1,否则返回0;
原型:⬇️
include
include
int myIsnumber(int c);
int main ()
{
char chr = '\0';
scanf("%c",&chr);
if(myIsnumber(chr))
{
printf("%c是数字字符\n",chr);
}
else
{
printf("%c不是数字字符\n",chr);
}
return 0;
}
int myIsnumber(int c)
{
if(c >= '0' && c <= '9')
{
return 1;
}
return 0;
}
isupper() 判断是否大写字母
如果是大写字母返回1,否则返回0 ;
原型 ⬇️
include
include
int myIsupper(int c);
int main ()
{
char chr = '\0';
scanf("%c", &chr);
if(myIsupper(chr))
{
printf("%c是大写\n",chr);
}
else
{
printf("%c不是大写\n",chr);
}
return 0 ;
}
int myIsupper(int c)
{
if(c >= 'A' && c <= 'Z')
{
return 1;
}
return 0;
}
islower() 判断是否是小写字母
#include
int
islower(int c);
如果是小写字母返回1,否则返回0
toupper() 小写转大写字母
#include
int
toupper(int c)
字母小写转成大写
原型:⬇️
include
include
int myToupper(int c);
int main()
{
char chr1 = '\0';
char chr2 = '\0';
scanf("%c",&chr1);
chr2 = myToupper(chr1);
printf("chr2 = %c\n",chr2);
printf("chr1 = %c\n",chr1);
}
int myToupper(int c)
{
if(c >= 'a' && c <= 'z')
{
return (c - 32);
}
return c;
}
strlen() 字符串长度
#include
size t
strlen(const char *s);
size_t = = = =unsigned long
include
include
size_t myStrlen(const char* s);
int main ()
{
char str[128] = "";
gets(str);
printf("strlen(str) = %lu\n",myStrlen(str));
return 0;
}
size_t myStrlen(const char* s)
{
int sum = 0;
while(*s++)
{
sum++;
}
return sum;
}
strcpy() 字符串复制
#include
char *
strcpy(char *dst,cons char *src)
dst 是一个数组的首地址,src是一个字符串的首地址,函数负责将src 拷贝到dst中
include
include
char* myStrcpy(chardst,const charsrc);
int main ()
{
char str[128] = "";
char arr[128] = "";
char* tem = NULL;
gets(arr);
tem = myStrcpy(str,arr);
printf("str = %s\n",str);
printf("tem = %s\n",tem);
return 0;
}
char* myStrcpy(char* dst, const charsrc)
{
char p = dst;
//while(*dst++ = src++);
while(src)
{
*dst = *src;
dst++;
src++;
}
*dst = '\0';
return p;
}
strcat ()
s1是一个数组的首地址,s2是一个字符的首地址,函数负责s2追加到s1的末尾🔚
include
include
char* myStrcat(char* s1, const char* s2);
size_t myStrlen(const chars);
int main ()
{
char str1[128] = "abcd";
char str2[128] = "";
char temp = NULL;
gets(str2);
temp =myStrcat(str1,str2);
printf("str1 = %s\n",str1);
printf("temp = %s\n",temp);
return 0;
}
char* myStrcat(char* s1, const char* s2)
{
char* p = s1;
int len = myStrlen(s1);
s1 += len;
while(*s2)
{
*s1 = *s2;
s1++;
s2++;
}
*s1 = '\0';
return p;
}
size_t myStrlen(const char* s)
{
int sum = 0;
while(*s++)
{
sum++;
}
return sum;
}
strcmp() 字符串比较,比较两个字符串的大小,S1大返回正数,S2大返回负数,相等返回0
[注]首先比较两个字符串的第一个字符,谁的ascii大,谁就大。如果第一个字符相等,那么继续比较下一个字符。
绝对不能用关系运算符比较两个字符的大小,这是两个地址,没有任何意义(str >str2(错误的))
include
include
int myStrcmp(const char* s1, const char* s2);
int main ()
{
char str1[128] = "";
char str2[128] = "";
char ret = 0;
gets(str1);
gets(str2);
ret = myStrcmp(str1, str2);
if(ret > 0)
{
printf("str1 > str2");
}
else if
{
printf("str1 < str2");
}
else
{
printf("str1 = str2");
}
return 0;
}
int myStrcmp(const char* s1, const char* s2)
{
while((*s1 == *s2) && *s1)
{
s1++;
s2++;
}
return *s1 - *s2;
}
strchr()
strchr:在s这个字符串中寻找字符C第一次出现的为止(地址),如果找到字符C,返回C的地址,否则返回空(NULL)
include
include
char* myStrchr(const char* s,int c);
int main ()
{
char str[128] = "";
char chr = 's';
char* p = NULL;
gets(str);
p =myStrchr(str, chr);
if (p == NULL)
{
printf("在%s中没有找到%c\n",str, chr);
}
else
{
printf("在%s中找到%c\n",str, chr);
printf("p = %s\n",p);
}
return 0;
}
char* myStrchr(const char* s,int c)
{
const char* p = s;
int len = 0;
while(*p)
{
if(c == p)
{
return (char)p;
}
p++;
}
return NULL;
}
strrchr():在S这个字符串中,C最后一次出现的位置(地址),如果找到字符C,返回c的地址,否则返回空(NULL);
strstr()
在字符串s1中寻找字符串s2,(至少s1要长等于s2),寻找子符串,如果找到返回子串第一次出现的位置,否则返回空(NULL)
include
include
int main ()
{
char str1[128] = "";
char* str2 = "sunck";
char* p =NULL;
gets(str1);
p = strstr(str1, str2);
if(p == NULL)
{
printf("%s不在%s中\n",str2, str1);
}
else
{
printf("%s在%s中\n",str2, str1);
}
return 0;
}
strtok()
字符串的风格,用sep这个字符串去风格str字符串
include
include
int main ()
{
char str1[128] = "";
char* p = NULL;
p =str1;
while (1)
{
p = strtok(p, " ");
if(!p)
{
break;
}
//-----------
printf("%s\n",p);
p = NULL;
}
gets(str1);
}
atoi()
将数字字符串转换为十进制数(遇到一个非数字字符立即停止)
include
include
int myAtoi(const char* str);
int myIsnumber(int c);
int main ()
{
char str[20] = "";
int num = 0;
gets(str);
num =myAtoi(str);
printf("num = %d\n",num);
return 0;
}
int myAtoi(const char* str)
{
int num = 0;
int flag = 1;
if (*str == '-')
{
flag = -1;
str++;
}
else if(*str == '+')
{
str++;
}
while (*str && myIsnumber(*str))
{
num = num * 10 + (*str - '0');
str++;
}
return num * flag;
}
int myIsnumber(int c)
{
if(c >='0' && c <= '9')
{
return 1;
}
return 0;
}