Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

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
0
effectivista
Asked:
effectivista
  • 4
  • 4
1 Solution
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now