位置:首页 > C > C语言strnset函数用法:将字符串前几个字符设为指定字符

C语言strnset函数用法:将字符串前几个字符设为指定字符

时间:2026-08-18  |  作者:骑光打字机  |  阅读:0

函数名:strnset

C语言strnset()函数:指定字符串的前几个字符都设为指定字符

基本信息

头文件:

函数原型如下:char *strnset(char *str, char ch, unsigned n);

功 能: 指定字符串的前几个字符都设为指定字符

参数说明

  • char *str 为要进行设定的字符串
  • char ch 为要设定的字符
  • unsinged n 为设定的字符个数

返回值

返回指向设定好的字符串的指针

使用注意

需要注意的是,这个函数会直接改动str本身的内容

也正因为如此,str只能传入字符数组,不能传入由字符串指针指向的字符串。

程序例

将string字符串的前n个字符替换成'!'

#include

#include

int main(void){

   char string[50] = "I like www.dotcpp.com";

   char letter = '!';

   printf("string before strnset: %sn", string);

   strnset(string, letter, 6);

   printf("string after  strnset: %sn", string);

   return 0;

}

运行结果

string before strnset: I like www.dotcpp.com
string after  strnset: !!!!!! www.dotcpp.com


免责声明:文中图文均来自网络,如有侵权请联系删除,心愿游戏发布此文仅为传递信息,不代表心愿游戏认同其观点或证实其描述。

相关文章

更多

精选合集

更多

大家都在玩

热门话题

大家都在看

更多