I know it is really nothing to some of you C-gods on the board, but i'm really proud of this code.
Code:
/* find 'ei' without string functions */
#include <stdio.h>
#include <stdlib.h>
int main(void){
char c,c1;
while ( (c = getchar()) !='#'){
if (c == 'e'){
c1 = getchar();
if (c1 == 'i'){
printf("Found ei\n");
}
}
}
system("pause");
return (0);
}
I was asked in my book to find all the occurances of 'ei' - without using pointers, arrays or even any string functions. And I have to say I am proud of my code. I am most impressed by the fact that I had to think logically. I am not very good at math and I turned to programming to help me develop my logic skills. I began programming in python which in my opinion is an unbelievable language. however the python code takes abstraction to the next level as is seen here
Code:
#test for ei
word = raw_input("Enter a sentence")
if "ei" in word:
print "ei is in sentence"
else:
print "No\n"
Nothing wrong with python it is a RAD,versatile and flexible language. However there was not much thinking going on. With C everyithing is so basic you have to think so low level, And that is an asset. Attemping this problem even allowed me to dabble around in my IDE and though it didn't work - allowed me to tinker around with functions - which is what i will be learning next
Code:
/* find 'ei' without string functions very bad code cause i never debugged it */
#include <stdio.h>
#include <stdlib.h>
void check4ei(void);
int main(void){
char ch = getchar();
int space;
while ( (ch != '#') ){
space = 0;
check4ei()
if ( ch == ' ' || ch == '\t' || ch =='\n' && space == 0){
space = 1;
}
else{
continue;
}
}
system("pause");
return (0);
}
/* function declararion */
void check4ei(void){
char c, c1;
int eicount = 0;
int space = 0;
c = getchar();
if (c == 'e' && space == 0){
c1 = getchar();
if (c1 == 'i' && space == 0){
eicount++;
}
}
}
Anyway the program can be refactored and should. It can easily be made to find a given sequence. That is of course once user input has been validated with some C functions.
Anyway thanks to the board for allowing me to rant and for all of your help in general when I have a probelm. I know it sounds gay, but i'm really grateful. I'm learning alot in many ways. It is 12:46 here in Canada, and I'm just so proud of myself.
C can be annoying as F**k sometimes, but when you get it. Its really rewarding.