473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is my code good?

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);
}
}
~
~

Nov 30 '05 #1
5 1180
po***********@gmail.com wrote:
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);
You forgot to #include <stdio.h>. If your compiler didn't warn you about
this, kick it. (Or just ask it to produce more warnings.)
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.
Nov 30 '05 #2
On 30 Nov 2005 14:36:17 -0800, in comp.lang.c ,
po***********@gmail.com wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 30 '05 #3
po***********@gmail.com wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
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> You left out <stdio.h>, which is needed for printf.
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.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Nov 30 '05 #4
On Wed, 30 Nov 2005 23:52:52 +0100, Skarmander
<in*****@dontmailme.com> wrote in comp.lang.c:
po***********@gmail.com wrote:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 1 '05 #5
po***********@gmail.com wrote:
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 }
}
~
~

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Dec 1 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
20
by: Clark | last post by:
Hi all. I'm looking for good C source code to study and be able to advance my C programming skills. Do you recomend any open source project in particular that in your opinion has good writen C...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
8
by: neilmcguigan | last post by:
I just wanted to list some reasons why I prefer inline code to code-behind. 1. you can fix some bugs more quickly. remote desktop into server, change the aspx file, and she's good to go. I'd say...
64
by: Bayazee | last post by:
hi can we hide a python code ? if i want to write a commercial software can i hide my source code from users access ? we can conver it to pyc but this file can decompiled ... so ...!! do you...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
4
by: shuisheng | last post by:
Dear All, I have a code developed by former employees. I extract some part of it as below: // definition of class CWNPrimitiveFace, it represent a face class CWNPrimitiveFace : public...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.