How to express string semantic (1)?

 String processing is very important in programming! How to express string semantic ?
There are many methods with doing it!   Some are listed  below!
(1) char array and pointer.
This  is always used in c. The advantage  is high time efficiency,beause it manipulate  memory address directly! However it  produce errors very easily and isn't very convenient! Some tips may be helpful!
#const string definition: 

  const   char   * str = " hello "  or 
 
const   char  str[] = " hello "

#string definition:

   char str[LEN]; //LEN is compling-time const!
   char *str=new char[ILEN]  //LEN is valid value!
   but don't forget delete []str;
#functions which relate to string, definined in string.h/stdio.h etc
 such as:
  strcmp,strlen,strcpy/strncpy etc! please refer to 
  http://www.cppreference.com/stdstring/index.html
  some function is worth learning! Attention please!
  sprintf() is useful! 

#include  < stdio.h >
  
int  sprintf(  char   * buffer,  const   char   * format,  );
  
char  string[ 50 ]; 
   sprintf( string, 
" Hello %s " ,world );

     The following code uses sprintf() to convert an integer into a string of characters: 

char  result[ 100 ];
   
int  num  =   24 ;
   sprintf( result, 
" %d " , num );              

   This code is similar, except that it converts a floating-point number into an array of characters: 

char  result[ 100 ];
   
int  num  =   24 ;
   sprintf( result, 
" %d " , num );              

snprintf() is like sprintf but with length argument!
(continuing...)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值