?
Solved

working with string in c

Posted on 2005-03-04
8
Medium Priority
?
256 Views
Last Modified: 2010-04-17
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
Comment
Question by:effectivista
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 375 total points
ID: 13458237
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13458539
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13458583
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!

 
LVL 35

Expert Comment

by:TimYates
ID: 13458589
ahhh, I see, you could be relying on the rollno char arry being initialized to all \0s
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13458900
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13458920
>>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
 
LVL 35

Expert Comment

by:TimYates
ID: 13459080
>> so the code SHOULD have been:

Which again is what I said...
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13459384
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question