Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Having problem..... (http://www.go4expert.com/forums/having-problem-t21067/)

Fatima Khan 21Feb2010 13:33

Having problem.....
 
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.

Gene Poole 21Feb2010 20:46

Re: Having problem.....
 
You'll have to give some more details.

virxen 22Feb2010 04:51

Re: Having problem.....
 
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;
}


xpi0t0s 22Feb2010 13:45

Re: Having problem.....
 
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.

vivekraj 23Feb2010 11:29

Re: Having problem.....
 
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..........

sganesh 23Feb2010 12:17

Re: Having problem.....
 
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;
}



All times are GMT +5.5. The time now is 09:29.