Output the ABCs using C programming
i know this is a simple task but i want to know how can i output the ABCs.
i havent programed for a while and i have been wasting time gaming and i would really like to get back in to programming. but for fun and as a warm up i want to do this project. but i have no idea how i would start.
what i want to to program is this:
------------------------------------
OUTPUT:
------------------------------------
a
ab
abc
abcd
abcde
...
f - u (dont want to waste room)
...
abcdefghijklmnopqrstuv
abcdefghijklmnopqrstuvx
abcdefghijklmnopqrstuvxy
abcdefghijklmnopqrstuvxyz
------------------------------------
how would i achive this.
i know i will be using a for loop and have a char var but i have no idea how i would start this?
There are many ways. Here's my first shot:
NSMutableString* out = [NSMutableString string];
for ( char ch = 'a' ; ch <= 'z' ; ch++ ) {
[out appendFormat:@"%c",ch] ;
NSLog(@"%@",out) ;
}
Oops. I just noticed that you wanted a solution in C - and I've given you Obj/C. In C it would be something like:
int main()
{
char ch,k ;
for ( k = 'a' ; k <= 'z' ; k++ ) {
for ( ch = 'a' ; ch <= k ; ch ++ )
printf("%c",ch);
printf("\n");
}
return 0;
}
// or
#include <stdio.h>
int main()
{
char string[100] ;
int l = 0 ; // length of string
char ch ;
for ( ch = 'a' ; ch <= 'z' ; ch++ ) {
string[l] = ch ; // add a character
string[++l] = 0 ; // string must be 0 terminated
puts(string) ; // print it
}
return 0;
}
#include <stdio.h>
int main()
{
char string[100] ;
int l = 0 ; // length of string
char ch ;
for ( ch = 'a' ; ch <= 'z' ; ch++ ) {
string[l] = ch ; // add a character
string[++l] = 0 ; // string must be 0 terminated
puts(string) ; // print it
}
return 0;
}
-clanmills
thanks so much mate. i want the code for this so i can use the algorithm that it is in it.
int number_gen(){
printf("00000000\n");
char eight_bit;
char string[100];int l = 0;
for(eight_bit = '00000001'; eight_bit <= '99999999'; eight_bit++){
string[l] = eight_bit;
string[++l] = 0;
puts(string);
}
}int main(){
number_gen();return 0;
}
-zophtx
----WARNING:THIS CODE IS VERY DANGEROUS-------
when i ran this code the computer started beeping and i couldnt quit out of the cmd line.
i havent tried it on a mac yet but can you try this code.
what im try to produce:
00000000
00000001
00000002
00000003
..............
..............
99999996
99999997
99999998
99999999
but instead computer beeps... CPU/RAM over doing?
The code isn't dangerous - you've misused it and have a buffer overflow. You are writing 100,000,000 bytes into a string of 100 bytes.
If you want to print 000 ... 999, you do something like:
int i ;
for ( i = 0 ; i < 1000 ; i++ )
printf("%09d\n",i) ;
'C' isn't a language for casual use. You'll have to learn the language before you can modify other folks code. There are lots of courses (both on-line and at Colleges). There are many books. I personally learned 'C' from this book: http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)
i figure i over flowed something.. i just didnt know what.. but i get the overflow from have the 000.. before the one i quess the computer cant understand it in that terms. but it doesn't hurt to try new things even if it could ruin my computer.. lol.
i have a c book. and a c pocket book. i have the Steve Kochan book. i have his C and OBj-C book.
i am thankful for you help and time. i hope my stupidity doesn't get you mad..
The Obj/C book by Stephen Kochan is very well written. In fact - it's possibly the best book from which to learn C and Obj/C. However Cocoa isn't Obj/C and when you graduate from the language (C or Obj/C) to Cocoa (the frameworks), I recommend Aaron Hillegass' book (3rd Edition). And may I plug my Cocoa tutorials at: http://clanmills.com/articles/cocoatutorials
You're on a long road to understanding all of this stuff. You'll have happy (and unhappy) days along that road and I wish every success. If I can help you, please drop by the forum, or email me personally and I'll be happy to help.
--------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(){
int i = 25;
char low_abc[] = {'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'};
char CAP_ABC[] = {'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'};
int a, b, c;
FILE *wFile = fopen("1-3LetterCombintion.txt", "w");
for(a = 0; a <= i; a++){
fprintf(wFile, "%c\n", low_abc[a]);
fprintf(wFilet, "%c\n", CAP_ABC[a]);
for(b = 0; b <= i; b++){
fprintf(wFile, "%c%c\n", low_abc[a], low_abc[b]);
fprintf(wFile, "%c%c\n", CAP_ABC[a], CAP_ABC[b]);
fprintf(wFile, "%c%c\n", CAP_ABC[a], low_abc[b]);
fprintf(wFile, "%c%c\n", low_abc[a], CAP_ABC[b]);
for(c = 0; c <= i; c++){
fprintf(wFile, "%c%c%c\n", low_abc[a], low_abc[b], low_abc[c]);
fprintf(wFile, "%c%c%c\n", CAP_ABC[a], CAP_ABC[b], CAP_ABC[c]);
fprintf(wFile, "%c%c%c\n", CAP_ABC[a], low_abc[b], low_abc[c]);
fprintf(wFile, "%c%c%c\n", low_abc[a], low_abc[b], CAP_ABC[c]);
fprintf(wFile, "%c%c%c\n", low_abc[a], CAP_ABC[b], CAP_ABC[c]);
fprintf(wFile, "%c%c%c\n", CAP_ABC[a], CAP_ABC[b], low_abc[c]);
fprintf(wFile, "%c%c%c\n", CAP_ABC[a], low_abc[b], CAP_ABC[c]);
fprintf(wFile, "%c%c%c\n", low_abc[a], CAP_ABC[b], low_abc[c]);
}
}
}
fclose(wFile);
}
--------------------------------------------------------------------------------------------------------------------------------------------
is there any easier way of doing this than have to do alot of "fprintf" statements?
You can probably rewrite this with one or two printf statements. I'm not sure what the purpose of this is, however the function toupper(c) is probably all you need to get the capitalization. So you can use a loop this this:
for ( c = 'a' ; c <= 'z' ; c++ ) {
printf("%c%d\n",c,toupper(c)) ;
}