So I want to make a program in C which prints the initals of a name but facing one problem . My program should not print .
as the last character, and I have tried this:
#include <stdio.h>
#include <string.h>
int main(){
printf("Enter your Name : \n");
char name[25];
gets(name);
int i;
printf("%c.",name[0]);
for(i=0;name[i]!='\0';i++){
if(name[i]==' '){
printf("%c",name[i+1]);
if(i<strlen(name)){
printf(".");
}
}
}
return 0;
}
but while running this program the with example input Satyajit Kumar Ghosh
, it is giving output "S.K.G." I am not getting why it is printing "." at the end as I give the condition
if(i<strlen(name)
The program should be as below. You were printing dot after character while detecting white space, so code added last dot after G instead before it.
#include <stdio.h>
#include <string.h>
int main(){
printf("Enter your Name : \n");
char name[25];
gets(name);
int i;
printf("%c",name[0]);
for(i=0;name[i]!='\0';i++){
if(name[i]==' '){
printf(".");
printf("%c",name[i+1]);
}
}
return 0;
}