Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // function returns the total number of space in the string
- // if name = Sourav Ahmed Subho
- // return value is 2
- int get_space_count(char* name)
- {
- int space_count = 0,i;
- for(i = 0; name[i] != '\0';i++){
- if(name[i] == ' ') space_count++;
- }
- return space_count;
- }
- int main(int argc, char const *argv[])
- {
- char name[100];
- int res,i,flag=1;
- printf("Enter Your name: ");
- scanf("%[^\n]",&name);
- res = get_space_count(name);
- i = 0;
- // loop will be from upto the last space. therefore 2 space will be reduce by 1
- // 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.
- while(res > 0){
- //first the initial will be printed.
- if(flag == 1){
- // check will be the last initial
- // if res is 1 then its the last last initial so no (dot) only a space
- if(res == 1){
- printf("%c ",name[i]);
- }
- // else after initial their will be a (dot) added.
- else{
- printf("%c.",name[i]);
- }
- flag = 0;
- }
- if(name[i] == ' '){
- flag = 1;
- res --;
- }
- i++;
- }
- // after all the initial is printed, the left over sir name is printed.
- while(name[i]!='\0'){
- printf("%c",name[i++]);
- }
- printf("\n\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment