Weird shapes as output( Strings)- C language -


so have problem. have separate first name, last name , hostname of email. example:

zephyr.extreme@gmail.com>> input

output=

first name= zephyr

last name= extreme

host name= gmail.com

i not getting desired result. getting weird shapes output.

code:

#include <stdio.h>  int main() { char email[40], first[20],last[20],host[30]; printf("enter email= "); gets(email); int i; while(email[i]!='\0') {     while(email[i]!='.')     {         first[i]=email[i];         i++;     }     while(email[i]!='@')     {         last[i]=email[i];         i++;     }     while(email[i]!='\0')     {         host[i]=email[i];         i++;     } } puts(first); puts(last); puts(host); } 

assuming format first.last@host..., use code:

#include <stdio.h> #include <string.h>  int main() {     char email[40], first[20],last[20],host[30],name[40];     int firstdot,atsymbol;     int i;     int length;     char *token;      printf("enter email= ");     gets(email);     length = strlen(email);     for(i=0;i<length;i++){          if(email[i]=='.')         {             firstdot = i;         }         else if(email[i]=='@')         {             atsymbol = i;         }     }     strncpy(name,email,atsymbol);     name[atsymbol]= '\0';        token = strtok(name, ".");         /* walk through other tokens */        while( token != null )         {           printf( "%s\n", token );           token = strtok(null, ".");        }        strncpy(host,email+atsymbol,length-atsymbol);     host[length-atsymbol] = '\0';     puts(host); } 

Comments