C语言学习字符串函数

一.字符输入输出

putchar
• int putchar(int c);
• 向标准输出写一个字符
• 返回写了几个字符,EOF(-1)表示写失败

getchar
• int getchar(void);
• 从标准输入读入一个字符
• 返回类型是int是为了返回EOF(-1)
• Windows—>Ctrl-Z
• Unix—>Ctrl-D

#include <stdio.h>
int main(int argc,char const *argv[])
{
    int ch;
    
    while ( (ch = getchar()) != EOF ){  //输入int类型 
        putchar(ch);    //输出int类型 
    }
    printf("EOF
"); //ctrl+z 输出
    return 0;
}

二.标准库中的字符串函数

string.h
• strlen
• strcmp
• strcpy
• strcat
• strchr
• strstr

strlen
• size_t strlen(const char *s);
• 返回s的字符串长度(不包括结尾的0)

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

size_t mylen(const char* s)  //用函数来代表strlen 
{
//    int cnt = 0;
    int idx = 0;  
    while (s[idx] != ''){
      idx++;
 //        cnt++;
  }
  return idx;
} 

int main(int argc, char const *argv[])
{
    char line [] = "Hello";
    printf("strlen=%lu
",strlen(line));//返回字符长度 
    printf("sizeof=%lu
",sizeof(line)); //还要算上最后的0 
    
    return 0;
 } 

 strcmp
• int strcmp(const char *s1, const char *s2);
• 比较两个字符串,返回:

• 0:s1==s2
• >0:s1>s2
• <0:s1<s2

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

int mycmp(const char* s1, const char* s2)
{
    int idx = 0;
    while ( s1[idx] == s2[idx] && s1[idx] !=''){
//        if ( s1[idx] != s2[idx] ) {
//        break;
//        }else if ( s1[idx] =='') {
//            break;
//        }
        idx  ++;
    }
    return s1[idx] - s2[idx];
}
int main(int argc, char const *argv[])
{
    char s1[] = "abc";
    char s2[] = "Abc";
    printf("%d
",strcmp(s1,s2));
    printf("%d
",'a'-'A');
    
    return 0;
 } 
 // while ( *s1 ==*s2 && *s1 != ''){
//         s1++;
//         s2++;
// }
// return *s1 - *s2;

 strcpy
• char * strcpy(char *restrict dst, const char *restrictsrc);

• 把src的字符串拷贝到dst
• restrict表明src和dst不重叠(C99)
• 返回dst
• 为了能链起代码来

#include <stdio.h>
#include <string.h>
char* mycpy(char* dst,const char* src) 
{
    int idx=0;
    while (src[idx] !=''){ //数组版本 实现 
        dst[idx]=src[idx];
        idx ++;
    }
    dst[idx] = '';
    return dst;
}


int main(int argc, char const *argv[])
{
    char s1[] = "abc";
    char s2[] = "Abc";
    strcpy(s1,s2);
    
    return 0;
 } 
// char* ret =dst; 
// while(*src){   //指针版本实现 
//     *dst++=*src++;
//     dst++;
//     src++;
// }
//     *dst = '';
//     
//     return ret;

 复制一个字符串
char *dst = (char*)malloc(strlen(src)+1);
strcpy(dst, src);

strcat
• char * strcat(char *restrict s1, const char *restrict s2);

• 把s2拷贝到s1的后⾯面,接成一个长的字符串
• 返回s1
• s1必须具有足够的空间

#include <stdio.h>
 
void strcmpmyself(char str1[1000],char str2[1000])
{
    int i,j ;
    for(i=0;str1[i]!='';i++);
     
    for(j=0;str2[j]!='';j++)
        str1[i+j]=str2[j];
}
 
int main()
{
    char str1[1000],str2[1000];
    printf("请输入字符串1:");
    gets(str1);
    printf("请输入字符串2:");
    gets(str2);
    strcmpmyself(str1,str2);
    printf("连接后的字符串为:%s",str1);
    return 0 ;
}

//int main(int argc, char const *argv[])
//{
//    char s1[] = "abc";
//    char s2[] = "Abc";
//    printf("%s
",strcat(s1,s2));
//    
//    return 0;
// } 

@strcpy和strcat都可能出现安全问题,目的地址没有足够空间就会出现问题。

安全版本
• char * strncpy(char *restrict dst, const char *restrict src, size_t n);

• char * strncat(char *restrict s1, const char *restrict s2, size_t n);

• int strncmp(const char *s1, const char *s2, size_t n);

字符串中找字符
• char * strchr(const char *s, int c);
• char * strrchr(const char *s, int c);
• 返回NULL表⽰示没有找到

• 如何寻找第2个?

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

//int main(int argc,char const *argv[])
//{
//    char s[] ="hello";
//    char *p = strchr(s,'l');
////    p = strchr(p+1,'l');
//    char *t =(char*)malloc(strlen(p)+1); //字符长度+1 
//    strcpy(t,p);
//    
//    printf("%s
",p);
//    free(t); //释放内存 
//    return 0;
//}


int main(int argc,char const *argv[])
{
    char s[] ="hello";
    char *p = strchr(s,'l');

char c =*p;  // 取出he 
*p = '';
char *t = (char*)malloc(strlen(s)+1);
strcpy(t,s);
    printf("%s
",t);
    free(t); //释放内存 
    return 0;
}
原文地址:https://www.cnblogs.com/guoweilf/p/11491931.html