Is my code good?

P

poison.summer

Here is my short code to implement such function:
Check whether the string starts with "12", if not
add "12" in front of the string.

Is the code good? or any better way to do it?

Thanks a lot!

#include <string.h>
int main()
{
char A[10];
char B[10];
strcpy(A,"abcdef\0");
printf("A=(%s)\n",A);
if((A[0]=='1')&&(A[1]=='2'))
printf("A=(%s)\n",A);
else
{
B[0]='1';
B[1]='2';
B[2]='\0';
strcat(B,A);
printf("B=(%s)",B);
}
}
~
~
 
S

Skarmander

Here is my short code to implement such function:
Check whether the string starts with "12", if not
add "12" in front of the string.

Is the code good? or any better way to do it?

Thanks a lot!

#include <string.h>
int main()
{
char A[10];
char B[10];

Statically allocated buffers are easy to work with, but they shouldn't be
used if there's no practical limit for your input size. A string could be of
any length.
strcpy(A,"abcdef\0");

The string literal automatically ends in a NUL. You don't need to add one
yourself. "abcdef" will do fine.
printf("A=(%s)\n",A);

if((A[0]=='1')&&(A[1]=='2'))
printf("A=(%s)\n",A);
else
{
B[0]='1';
B[1]='2';
B[2]='\0';
strcat(B,A);
printf("B=(%s)",B);
}
}

Your code "works", as long as the "strings" are always within bounds, but
from the way you describe the problem, you are probably supposed to
implement a function that looks like this:

void ensure_12_prefix(char* s) { ... }

Where s is a string with enough room to add "12" in front of it if it's not
already there. Try your hand at that one.

Of course, I'm just guessing as to the actual intent of the assignment. It's
a bit vague. "The string"? What string?

S.
 
M

Mark McIntyre

On 30 Nov 2005 14:36:17 -0800, in comp.lang.c ,
Here is my short code to implement such function:
Check whether the string starts with "12", if not
add "12" in front of the string.

Is the code good? or any better way to do it?


use strncmp()

// assumes sensible definitions of a and b
if(strncmp(a, "12",2)
sprintf(b, "12%s", a);
char A[10];
char B[10];

Bad style: upper-case identifiers are generally reserved for Macros -
variables should be lowercase or mixed case.
 
R

Randy Howard

(e-mail address removed) wrote
(in article
Here is my short code to implement such function:

Actually, it does it all in main, but it does 'function' in the
sense of sending stuff to stdout I suppose. If it would compile
that is. I suspect it is probably supposed to take a string
passed on the command line (or be an actual function that works
on a string passed in) before you turn it in.
Check whether the string starts with "12", if not
add "12" in front of the string.

Is the code good? or any better way to do it?

How many days until your homework is due?
Thanks a lot!

#include <string.h>
int main() try int main(void)
{
char A[10];
char B[10];
strcpy(A,"abcdef\0");

Google for 'buffer overflow'. This program has it written all
over it, especially if you make it more general. Also, think
about
printf("A=(%s)\n",A);
if((A[0]=='1')&&(A[1]=='2'))
printf("A=(%s)\n",A);
else
{
B[0]='1';
B[1]='2';
B[2]='\0';
strcat(B,A);
printf("B=(%s)",B);
}
You need either
return 0;
or
return EXIT_SUCCESS; /* and stlib.h */
here.

You also need to take a long look at how this works (or does
not) work when you use input other than 'abcdef'.

If you make this into a more general function to prefix strings,
you're going to have to be much more careful.
 
J

Jack Klein

Here is my short code to implement such function:
Check whether the string starts with "12", if not
add "12" in front of the string.

Is the code good? or any better way to do it?

Thanks a lot!

#include <string.h>
int main()
{
char A[10];
char B[10];

Statically allocated buffers are easy to work with, but they shouldn't be
used if there's no practical limit for your input size. A string could be of
any length.

This is the kind of improper terminology that confuses newbies. The
two arrays you refer to are most certainly not 'statically allocated',
which would mean, in C, that they have static storage duration.

These arrays have automatic storage class and duration.

Please refer to this as "fixed size", and don't confuse newbies.
 
M

Michael Mair

Here is my short code to implement such function:
Check whether the string starts with "12", if not
add "12" in front of the string.

Is the code good? or any better way to do it?

Thanks a lot!

#include <string.h>
int main()
{
char A[10];
char B[10];
strcpy(A,"abcdef\0");
printf("A=(%s)\n",A);
if((A[0]=='1')&&(A[1]=='2'))
printf("A=(%s)\n",A);
else
{
B[0]='1';
B[1]='2';
B[2]='\0';
strcat(B,A);
printf("B=(%s)",B);

In addition to what the other answers noted:
You need a '\n' at the end of the last output if you
want to be sure you actually get it out.

-Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top