Having problem.....

Go4Expert Member
21Feb2010,13:33   #1
Fatima Khan's Avatar
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.
Contributor
21Feb2010,20:46   #2
Gene Poole's Avatar
You'll have to give some more details.
Pro contributor
22Feb2010,04:51   #3
virxen's Avatar
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;
}
Fatima Khan like this
Mentor
22Feb2010,13:45   #4
xpi0t0s's Avatar
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.
Light Poster
23Feb2010,11:29   #5
vivekraj's Avatar
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..........
Light Poster
23Feb2010,12:17   #6
sganesh's Avatar
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;
}
Fatima Khan like this