working with string in c

my code is like

char rollno[5];
char name[20];
char mesg[]="12345expert"
int k;
k= strlen(mesg);
strncpy(rollno,mesg,5);
strncpy(name,mesg+5,k);
printf("%s",rollno);
printf("%s",name);

and gives output like
12345@23A343
expert

why i don't  know . but it works well in another program.
pls help  i m
getting here unnecessary chars with rollno

dhiraj
effectivistaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
I *think* it's because you haven't null terminated your strings...

I haven't done C for a while, but I *think* you need:

char rollno[ 6 ];
char name[20];
char mesg[]="12345expert"
int k;
k= strlen(mesg);
strncpy(rollno,mesg,5);
rollno[ 5 ] = '\0' ;
strncpy(name,mesg+5,k);
printf("%s",rollno);
printf("%s",name);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulCaswellCommented:
You need:

char rollno[6]; // 5 is not enough for 5 characters and a terminator.
char name[20];
char mesg[]="12345expert"
int k;
k= strlen(mesg);
strncpy(rollno,mesg,5);
strcpy(name,mesg+5); // Just copy the rest.
printf("%s",rollno);
printf("%s",name);

You should now get
"12345expert"

which may still confuse you until you change it to:

printf("%s\n",rollno);
printf("%s\n",name);

Paul
0
TimYatesCommented:
which is basically what I said...

Does

    strncpy(rollno,mesg,5);

add the \0 in then?  I didn't realise that...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TimYatesCommented:
ahhh, I see, you could be relying on the rollno char arry being initialized to all \0s
0
PaulCaswellCommented:
strncpy will NOT append a '\0' if it copied the specified number of characters.

What asker probably should have done was:

char rollno[6]; // 5 is not enough for 5 characters and a terminator.
char name[20];
char mesg[]="12345expert"
sscanf ( mesg, "%5s%s", rollno, name );
printf("%s\n",rollno);
printf("%s\n",name);

But I was trying to fix the code, not rewrite it.

Paul
0
PaulCaswellCommented:
>>ahhh, I see, you could be relying on the rollno char arry being initialized to all \0s
But that's a good point too, so the code SHOULD have been:

char rollno[6]; // 5 is not enough for 5 characters and a terminator.
char name[20];
char mesg[]="12345expert"
int k;
k= strlen(mesg);
strncpy(rollno,mesg,5);
rollno[5] = '\0'; // Just in case.
strcpy(name,mesg+5); // Just copy the rest.

but I stand by my suggestion that sscanf would be a better tool here.

Paul
0
TimYatesCommented:
>> so the code SHOULD have been:

Which again is what I said...
0
PaulCaswellCommented:
Tim,

I offer a complete and utter retraction. The imputation was totally without basis in fact and was in no way fair comment and was motivated purely by malice, and I deeply regret any distress that my comments may have caused you or your family, and I hereby undertake not to repeat any such slander at any time in the future.

Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.