C语言基础——字符串操作


字符串操作

在C程序中使用字符串,不可以使用操作符来操作字符串,应该使用一组标准函数,C标准库中有对于使用字符串操作的一组函数(需要包含头文件string.h)

字符串操作函数

strlen 函数

size_t strlen(const char *str)
参数

  • str – 这是字符串的长度要计算的。
    返回值
  • 这个函数返回字符串的长度。
#include <stdio.h>
#include <string.h>

int main ()
{
   char str[50];
   int len;

   strcpy(str, "This is yiibai.com");

   len = strlen(str);
   printf("Length of |%s| is |%d|
", str, len);
   
   return(0);
}
  • 输出
Length of |This is yiibai.com| is |26|

strcat 函数

char *strcat(char *dest, const char *src)
参数

  • dest – 这是目标数组,它应该包含一个C字符串,大到足以包含级联的结果字符串的指针。
  • src – 这是要追加的字符串。这不应该重叠的目的地。
    返回值
  • 这个函数返回一个指针生成的字符串dest。
#include <stdio.h>
#include <string.h>

int main ()
{
   char src[50], dest[50];

   strcpy(src,  "This is source");
   strcpy(dest, "This is destination");

   strcat(dest, src);

   printf("Final destination string : |%s|", dest);
   
   return(0);
}
  • 输出
Final destination string : |This is destinationThis is source|

strncat 函数

char *strncat(char *dest, const char *src, size_t n)

参数

  • dest – 这是,它应该包含一个C字符串,大到足以包含级联产生附加的空字符的字符串,其中包括目标数组的指针。
  • src – 这是要追加的字符串。
  • n – 这是要追加的字符的最大数目。
    返回值
  • 这个函数返回一个指针生成的字符串dest。
#include <stdio.h>
#include <string.h>

int main ()
{
   char src[50], dest[50];

   strcpy(src,  "This is source");
   strcpy(dest, "This is destination");

   strncat(dest, src, 15);

   printf("Final destination string : |%s|", dest);
   
   return(0);
}
  • 输出
Final destination string : |This is destinationThis is source|

strcmp 函数

int strcmp(const char *str1, const char *str2)
参数

  • str1 – 这是第一个要比较的字符串。
  • str2 – 这是第二个的字符串进行比较。
    返回值
  • 这个函数的返回值如下:
  • 如果返回值<0,则表明str1小于str2
  • 如果返回值,如果> 0,则表明str2 小于 str1
  • 如果返回值= 0,则表明str1 等于str2
#include <stdio.h>
#include <string.h>

int main ()
{
   char str1[15];
   char str2[15];
   int ret;


   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");

   ret = strcmp(str1, str2);

   if(ret > 0)
   {
      printf("str1 is less than str2");
   }
   else if(ret < 0) 
   {
      printf("str2 is less than str1");
   }
   else 
   {
      printf("str1 is equal to str2");
   }
   
   return(0);
}
  • 输出
str1 is less than str2

strncmp 函数

int strncmp(const char *str1, const char *str2, size_t n)
参数

  • str1 – 这是第一个要比较的字符串。
  • str2 – 这是第二个进行比较的字符串。
  • n – 最大字符数进行比较。
    返回值
  • 如果返回值<0,则表明str1小于str2
  • 如果返回值,如果> 0,则表明str2 小于 str1
  • 如果返回值= 0,则表明str1 等于str2
#include <stdio.h>
#include <string.h>

int main ()
{
   char str1[15];
   char str2[15];
   int ret;


   strcpy(str1, "abcdef");
   strcpy(str2, "ABCDEF");

   ret = strncmp(str1, str2, 4);

   if(ret > 0)
   {
      printf("str1 is less than str2");
   }
   else if(ret < 0) 
   {
      printf("str2 is less than str1");
   }
   else 
   {
      printf("str1 is equal to str2");
   }
   
   return(0);
}
  • 输出
str1 is less than str2

strcpy 函数

char *strcpy(char *dest, const char *src)
参数

  • dest – 这就是指针的内容将被复制到目标数组。
  • src – 这是要复制的字符串。
    返回值
  • 一个指向目标字符串dest
#include <stdio.h>
#include <string.h>

int main()
{
   char src[40];
   char dest[100];
  
   memset(dest, '', sizeof(dest));
   strcpy(src, "This is yiibai.com");
   strcpy(dest, src);

   printf("Final copied string : %s
", dest);
   
   return(0);
}
  • 输出
Final copied string : This is yiibai.com

strncpy 函数

char *strncpy(char *dest, const char *src, size_t n)
参数

  • dest – 指向用于存储复制内容的目标数组。
  • src – 要复制的字符串。
  • n – 要从源中复制的字符数。
    返回值
  • 该函数返回最终复制的字符串。
#include <stdio.h>
#include <string.h>

int main()
{
   char src[40];
   char dest[12];
  
   memset(dest, '\0', sizeof(dest));
   strcpy(src, "This is runoob.com");
   strncpy(dest, src, 10);

   printf("最终的目标字符串: %s\n", dest);
   
   return(0);
}
  • 输出
最终的目标字符串: This is ru

memset 函数

void *memset(void *str, int c, size_t n)
参数

  • str – 这是来填充的内存块的指针。
  • c – 这是要设置的值。作为一个int值传递,但使用这个值的无符号字符型转换函数填充的内存块。
  • n – 这是要设置的值的字节数。
    返回值
  • 这个函数返回一个指针,指向的内存区域str。
#include <stdio.h>
#include <string.h>

int main ()
{
   char str[50];

   strcpy(str,"This is string.h library function");
   puts(str);

   memset(str,'$',7);
   puts(str);
   
   return(0);
}
  • 输出
This is string.h library function
$$$$$$$ string.h library function

strstr 函数

char *strstr(const char *haystack, const char *needle)
参数

  • haystack – 这是主要的C字符串进行扫描。
  • needle – 这是小haystack中字符串内被搜索的字符串。
    返回值
  • 这个函数返回一个指针指向第一次出现在草垛整个针,或一个空指针指定的字符序列,如果序列是不存在haystack中。

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

int main()
{
const char haystack[20] = “TutorialsYiibai”;
const char needle[10] = “Yiibai”;
char *ret;

ret = strstr(haystack, needle);

printf("The substring is: %s
", ret);

return(0);
}

  • 输出
The substring is: Yiibai

sprintf 函数

具体参考:https://www.yiibai.com/c_standard_library/c_function_sprintf.html

  • 示例:
#include <stdio.h>
#include <math.h>

int main()
{
   char str[80];

   sprintf(str, "Value of Pi = %f", M_PI);
   puts(str);
   
   return(0);
}
  • 输出
Value of Pi = 3.141593

sscanf 函数

具体参考:https://www.yiibai.com/c_standard_library/c_function_sscanf.html

  • 示例:
#include <stdio.h>
#include <stdlib.h>

int main()
{
   int day, year;
   char weekday[20], month[20], dtm[100];

   strcpy( dtm, "Saturday March 25 1989" );
   sscanf( dtm, "%s %s %d  %d", weekday, month, &day, &year );

   printf("%s %d, %d = %s
", month, day, year, weekday );
    
   return(0);
}
  • 输出:
March 25, 1989 = Saturday
发布了78 篇原创文章 · 获赞 25 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_37596943/article/details/103979521