linux&c++ R&D

programing is a pleasure!

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...)


posted on 2007-04-18 23:31 丑石 阅读(282) 评论(1)  编辑 收藏 引用

Feedback

# re: How to express string synatic effectively(1)? 2007-04-19 03:49 天津大学计算机学院 常兴龙

写得不错,可以发在其它技术区,而非是新手区了。。。。  回复  更多评论   



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


My Links

Blog Stats

News

常用链接

留言簿(1)

随笔分类(13)

随笔档案(17)

文章档案(1)

相册

收藏夹(1)

Friends' blog

useful sites

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜