splash365

String Basic lecture 02

Aug 11th, 2020 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     ///Declaration
  6.     char s1[20]; // just declare the char array. Currently, in each position of the array is in garbadge value.
  7.  
  8.     printf("In declaration: \n");
  9.    
  10.     printf("Before input values in character array are: \n");
  11.     for(int i = 0; i<20; i++) printf("%d\n", s1[i]);
  12.     printf("\n.......................\n\n");
  13.     // Here we print the array. we'll notice that every postion will be set garbadge. Here we print the ASCII value of each character.
  14.    
  15.     /// note that, if we declare char s1[20] this globally, every position of the array will be initialized with Null (means 0 in integer).
  16.    
  17.     printf("Enter a string as a word: ");
  18.     scanf("%s", s1);
  19.     // %s can take only a word as an input.
  20.     // If we want to take whole line (including space), We must use "gets()". Syntax: gets(s1);
  21.     // both %s and gets() initialize the array upto the characters you've entered from keyboard.
  22.  
  23.     printf("After input values in character array are: \n");
  24.     for(int i = 0; i<20; i++) printf("%d\n", s1[i]);
  25.     printf("\n.......................\n\n");
  26.     // let's say, you've entered "MIST" as an input.
  27.     // It will take 4 position (from 0 to 3) in that char array. Rest of the positions willa remain garbadge.
  28.    
  29.     printf("You entered: %s /// printed using printf()\n", s1);
  30.     puts(s1); /// printed using puts()
  31.     printf("\n.......................\n\n");
  32.     // printf("%s",s); and puts(s); both print the character array up to Null characher ('\0').
  33.  
  34.     ///Definition
  35.     char s2[20] = "MIST";
  36.     // Here declare a char array and Initialize it's first 4 positions (0 to 3)
  37.    
  38.     printf("In definition values in character array are: \n");
  39.     for(int i = 0; i<20; i++) printf("%d\n", s2[i]);
  40.     printf("\n.......................\n\n");
  41.     // Here we print the array.
  42.     // We will notice that, rest of the positions (4 to 19) will be set NULL (means 0 in integer).
  43.  
  44.     puts(s2);
  45.    
  46.     return 0;
  47. }
  48.  
  49.  
Add Comment
Please, Sign In to add comment