-
May 21st, 2003, 01:55 PM
#1
Newcomer to C
Hi, I am somewhat of a new programmer and the only experience I have was using Java. But anways, I am now learning to write a C program and am unsure of some of the new syntaxs and restrictions. I looked at a sample program that prints out the alphabet from A-Z.
I am trying to understand this code, and when I rewrite it in my own words it works.
One thing I am unsure of is the structure of my loop(s), I can get it to print out all the characters but I am trying to make the letters of the alphabet take up 3 lines rather than all on 1 line. Any suggestions are gratefully appreciated.
Thanks in advance.
-
May 21st, 2003, 02:21 PM
#2
I don't really understand what your question is. If you're asking
how to go to the next line, you use the newline character: '\n'.
With this code:
Code:
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("line1\n");
printf("this is on line2\n");
printf("this is on line 3\n");
return 0;
}
The following will be output:
line1
this is on line2
this is on line 3
--Paul
-
May 21st, 2003, 08:29 PM
#3
Thanks for the reply. That kinda helped,
but my output is getting printed out with a For Loop
because my text is in an array? Any ideas..?
Thanks again
-
May 21st, 2003, 08:38 PM
#4
Hi spinC ,
Is this what you want?
for(int i=0;i<ArrayCount;i++)
{
printf("%s\n",Array[i]);
}
-
May 21st, 2003, 08:59 PM
#5
That code printed out the same element of my array 2 times on the line. Not exactly..... I'll post my code to show what I have and what I am trying to do.
int linenum=0;
int count=0;
char alpha[26] = "abcdefghijklmnopqrstuvxyz";
for(i = 0;i <= strlen(alpha); i++)
{
printf("%c\n", alpha[i]);
}
return 0;
This code prints out every letter on an individual line.
I am trying to have a variable where I can print out the entire
alphabet but I can specify how many lines to print it out on.
ie. abcdef
ghijklm
nopqrs
tuvwxy
z
5 lines
-
May 21st, 2003, 09:21 PM
#6
Hi spinC,
I am trying to understand what you want, but your ie. puzzled me.
ie. abcdef //6 characters
ghijklm //7 characters ??
nopqrs //6 characters
tuvwxy //6 characters
z //1 character
If you want to print out 6 characters one line,hope this helps,
int linenumber=0;
int count=0;
char alpha[26] = "abcdefghijklmnopqrstuvxyz";
for(int i=0;i<strlen(alpha);i++)
{
printf("%c",alpha[i]);
count++;
if(count==6)
{
printf("\n");
linenumber++;
count=0;
}
}
printf("\n\n");
if(count!=0)
{
linenumber++;
}
printf("%d lines\n",linenumber);
Last edited by Eric Leung; May 21st, 2003 at 09:30 PM.
-
May 21st, 2003, 09:36 PM
#7
This code example should print out the array in the specific no. of lines.
Code:
char alpha[]="abcdefghijklmnopqrstuvwxyz";
const int lines = 5;
const int len = strlen(alpha);
// Equivalent to len/lines and then round-up.
int charPerLine = (len + lines - 1)/lines;
for(int i=0; i<len; ++i)
{
if(i && (i%charPerLine) == 0)
printf("\n");
printf("%c", alpha[i]);
}
BTW, there is a mistake in the previous example,
char alpha[26] = "abcdefghijklmnopqrstuvxyz";
You need to allocate an extra space for '\0'.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
May 21st, 2003, 09:43 PM
#8
hmmmm.....I think what you posted might help but Im basically just trying to get it to print out the alphabet with a set number of characters per line, and this set number of characters per line is a number input by the user. Sorry I made a typo in the example so I see how it could be confusing. Thanks again for all the help though.
ie. if they enter the value of 5
abcde
fghij
klmno
pqrst
uvwxy
z
-
May 22nd, 2003, 10:19 AM
#9
try it:
Code:
void f(int s){
int i;
char alpha[]="abcdefghijklmnopqrstuvwxyz";
for(i = 0;i <= strlen(alpha); i++)
{
if(!(i%s)) printf("\n");
printf("%c", alpha[i]);
}
}
void main(){
f(5);
return;
}
-
May 22nd, 2003, 10:49 AM
#10
Originally posted by dav79
try it:
If they are using an ANSI C++ compiler, this won't compile. The main() function returns an int, not void.
Regards,
Paul McKenzie
-
May 22nd, 2003, 08:04 PM
#11
yup..
thank you all for the replies and help....
its good to go and prints out fine.
thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|