Vulpes

string_demo

Oct 22nd, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4.  
  5.  
  6. void string_function(){
  7.  
  8.    
  9.     int i;
  10.     char my_string[12]="hello world";
  11.     //this character array (my_string) is 12 characters long
  12.     //"hello" is 5 characters, " " (the space) is 1 character
  13.     //"world" is 5 characters. 5+1+5=11, not 12, so where is the last character?
  14.     //the last character is a 0 (or "\0") automatically added at the end of the character array
  15.     //I will now print out the string with it's corresponding ascii value http://www.asciitable.com/index/asciifull.gif
  16.    
  17.     printf("%s \n\n",my_string);
  18.     //this is what the string looks like normally
  19.    
  20.    
  21.     for (i=0; i<12; i++) {
  22.         printf("[%c] [%d] \n",my_string[i],my_string[i]);
  23.     }
  24.     printf("\n");
  25.    
  26.     //because of the fact that there is a 0 at the end of every string, you don't have to worry about the length of the string
  27.     //this time I will print out characters until I hit the "\0" at the end, (because that is the end of the string)
  28.    
  29.     i=0;
  30.     printf("\n");
  31.     while (my_string[i]!='\0') {
  32.         printf("%c",my_string[i]);
  33.         i++;
  34.     }
  35.     printf("\n");
  36.    
  37. }
  38.  
  39.  
  40.  
  41. int main () {
  42.  
  43.     string_function();
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment