A program that inputs one string of max length of 10. If the string is identical to "Virtual", output the message "Correct Password"; otherwise, output the first four characters in the message and the length of the message.
is this what you want? Code: #include <stdio.h> #include <string.h> int main(){ char data[10]; char temp[4]; char message[]="Correct Password"; printf("\ngive me data:"); scanf("%s",&data); if (strcmp(data,"Virtual")==0){ printf("%s",message); }else{ strncpy(temp,message,4); temp[4]='\0'; printf("%s",temp); printf("\nlength=%d",strlen(message)); } getchar();getchar(); return 0; }
Doing people's homework for them is less help than you think. It doesn't help them learn. They enter a good program so the teacher doesn't see that they're struggling. They delude themselves when they look at the program and think they could have figured it out (they didn't, and they couldn't, otherwise they would have done. But it looks easy when you see it done). Fast forward to the exam where they have no access to the internet: they will not have developed programming ability and will fail. Or if they do have access to the internet and you do their exam for them then they pass, but give employers (who are not so easily fooled) one more reason to discount academic results as nonsense because here's this twit with a pass grade who can't program. So by all means help people, but not by giving them completed code. If you could learn by looking at completed code samples then that's what the teachers would give out in the first place; they don't give you programming assignments just because they're sado-masochists, but because they know you LEARN programming by DOING programming.
If the word entered i equal to virtual,we need to print "Correct Password".Else,what you want to do?You want simple to print the first four characters of the message such as Corr and what you mean the length of the string.Is it 4 or what?Explain it correctly..........
I think this code will solve your problems, I did some of the modification in the program. Code: #include <stdio.h> #include <string.h> #include<malloc.h> int main(){ char *data = (char *)malloc(10); char *tmp; char temp[4]; char message[]="Correct Password"; data=getpass("\nGive me data:"); int len=strlen(data); if (strcmp(data,"Virtual")==0){ printf("%s",message); }else{ strncpy(temp,data,4); temp[4]='\0'; printf("Your password is wrong\n Data:%s length:%d\n",temp,len); } return 0; }