Monday, April 27, 2015

1. Khai báo
Strings

 char greeting1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};  

 char greeting2[] = "Hello";  

 char *greeting3 = "Hello";  

Note: Cuối chuỗi string phải có ki tự kết thúc '\0', ở cách khai báo thứ 2 &3 thì compiler tự động thêm '\0' vào cuối.


Array strings
 char greeting22[2][32] = {"Hello", "Hello2"}; /* array co 2 chuoi string, moi chuoi toi da 32 ki tu */  
 char *greeting32[32] = {"Hello", "Hello2"};  

Ex:
string.c
 #include <stdio.h>  
 #include <stdlib.h>  
 int main()  
 {  
   char greeting1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};  
   char greeting2[] = "Hello";  
   char *greeting3 = "Hello"; 
 
   printf("greeting1: %s \n", greeting1);  
   printf("greeting2: %s \n", greeting2);  
   printf("greeting3: %s \n", greeting3); 
 
   /* Array chuoi string */  
   char greeting22[2][32] = {"Hello", "Hello2"}; /* array co 2 chuoi string, moi chuoi toi da 32 ki tu */  
   char *greeting32[32] = {"Hello", "Hello2"};  
   printf("greeting22[0]: %s \n", greeting22[0]);  
   printf("greeting22[1]: %s \n", greeting22[1]);  
   printf("greeting32[0]: %s \n", greeting32[0]);  
   printf("greeting32[1]: %s \n", greeting32[1]);  
   return 0;  
 }  

Compile & Execute
 $ gcc string1.c   
 $ ./a.out   
 greeting1: Hello   
 greeting2: Hello   
 greeting3: Hello   
 greeting22[0]: Hello   
 greeting22[1]: Hello2   
 greeting32[0]: Hello   
 greeting32[1]: Hello2   

2. Strings function


S.N. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
7 int sprintf ( char * str, const char * format, ... );
Composes a string with the same text that would be printed if format was used on printf,
but instead of being printed, the content is stored as a C string in the buffer pointed by str.

Xem thêm tại đây.

Ex:
string2.c
 #include <stdio.h>  
 #include <string.h>  
 int main ()  
 {  
   char str1[12] = "Hello";  
   char str2[12] = "World";  
   char str3[12];  
   int len ; 
 
   /* copy str1 into str3 */  
   strcpy(str3, str1);  
   printf("strcpy( str3, str1) : %s\n", str3 ); 
 
   /* concatenates str1 and str2 */  
   strcat( str1, str2);  
   printf("strcat( str1, str2):  %s\n", str1 ); 
 
   /* total lenghth of str1 after concatenation */  
   len = strlen(str1);  
   printf("strlen(str1) : %d\n", len );
  
   sprintf(str3, "%d %s", 123, "Hello");  
   printf("sprintf str3: %s\n", str3 ); 
 
   return 0;  
 }  

Compile & Execute:
 $ gcc string2.c   
 $ ./a.out   
 strcpy( str3, str1) : Hello  
 strcat( str1, str2):  HelloWorld  
 strlen(str1) : 10  
 sprintf str3: 123 Hello  


Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -