Advertisement
Guest User

Strings demo

a guest
May 25th, 2013
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4.  
  5. void fun1()
  6. {
  7.     char one[10] = "whoareyou";
  8.     printf("%s \n", one);
  9.     one[4] = '\0';
  10.     printf("%s \n", one);
  11. }
  12.  
  13. void fun2()
  14. {
  15.     char two[7] = "mustang";
  16.     char three[4] = "dog";
  17.     char four[4] = "cat";
  18.     printf("%s\n", two);
  19. }
  20.  
  21. void fun3()
  22. {
  23.     char one[2];
  24.     char two[8] = "mustang";
  25.     char three[4] = "dog";
  26.     one[0] = 'a';
  27.     one[1] = 'b';
  28.     printf("%s\n", one);
  29. }
  30.  
  31. void fun4()
  32. {
  33.     char two[12] = "mustangherd";
  34.     two[7] = '\0';
  35.     two[5] = two[9];
  36.     two[6] = two[10];
  37.     printf("%s\n", two);
  38. }
  39.  
  40. void fun5()
  41. {
  42.     char howdy[] = "hello";
  43.     char snoopy[6];
  44.     char linus[5] = "linus";
  45.     char lucy[7] = "lucy";
  46.     int i;
  47.    
  48.     printf("%s\n", howdy);
  49.     printf("%s\n", lucy);
  50.  
  51.     printf("Enter a string: ");
  52.     scanf("%s", snoopy);
  53.  
  54.     printf("%s\n", snoopy);
  55.  
  56.     printf("-------");
  57.     for (i=0; i<3; i++)
  58.     {
  59.        printf("%c", snoopy[i]);
  60.     }
  61.     printf("\n");
  62.  
  63.     /*  lucy = "person";   wont' compile */
  64.     printf("%s\n", linus);
  65. }
  66.    
  67. void fun9()
  68. {
  69.     /* Surprisingly, the Hanly textbook doesn't explain that the
  70.      * compiler is smart enough to figure out how big of an array
  71.      * to allocate for a string */
  72.     char pres[] = "Barack Obama";
  73.     char greet[4] = "hi!";
  74.     printf("The president is: ");
  75.     printf("%s %s\n", pres,greet);
  76. }    
  77.    
  78. int main(void)
  79. {
  80.  
  81.     fun1();
  82.     fun2();
  83.     fun3();
  84.     fun4();
  85.     fun5();
  86.     fun9();
  87.     return 0;
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement