n0va_sa

Addressable Name[C Lang]

Apr 28th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // function returns the total number of space in the string
  5. // if name = Sourav Ahmed Subho
  6. // return value is 2
  7. int get_space_count(char* name)
  8. {
  9.     int space_count = 0,i;
  10.     for(i = 0; name[i] != '\0';i++){
  11.         if(name[i] == ' ') space_count++;
  12.     }
  13.  
  14.     return space_count;
  15. }
  16.  
  17. int main(int argc, char const *argv[])
  18. {
  19.     char name[100];
  20.     int res,i,flag=1;
  21.  
  22.     printf("Enter Your name: ");
  23.     scanf("%[^\n]",&name);
  24.  
  25.     res = get_space_count(name);
  26.     i = 0;
  27.     // loop will be from upto the last space. therefore 2 space will be reduce by 1
  28.     // 2 then 1(total run time is 2[for 2 it will run, for 1 it will run]) then 0. in zero the loop breaks.
  29.     while(res > 0){
  30.         //first the initial will be printed.
  31.         if(flag == 1){
  32.             // check will be the last initial
  33.             // if res is 1 then its the last last initial so no (dot) only a space
  34.             if(res == 1){
  35.                 printf("%c ",name[i]);
  36.             }
  37.             // else after initial their will be a (dot) added.
  38.             else{
  39.                 printf("%c.",name[i]);
  40.             }
  41.             flag = 0;
  42.         }
  43.         if(name[i] == ' '){
  44.             flag = 1;
  45.             res --;
  46.         }
  47.         i++;
  48.     }
  49.  
  50.     // after all the initial is printed, the left over sir name is printed.
  51.     while(name[i]!='\0'){
  52.         printf("%c",name[i++]);
  53.     }
  54.     printf("\n\n");
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment