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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章