[Browse] [Tag cloud]

Log on:
Powered by Elgg

James Marshall :: Friends blog

March 17, 2009

C is like a toolkit, but the only tools you get are the barest minimum you need to make all the other tools.

This is why it is exceptionally painful to process strings in C. The string.h library is exceptionally minimal - whereas other languages have functions such as substr to, for instance, get a substring within a string, C has none of this, you have to write it yourself.

 This isn't necessarily a bad thing - you can create very very fast things using C, as anything you create can be fit for purpose and completely efficient, however, as soon as you start to encroach on the world of generic applications, you risk falling into the slowness of other languages with other libraries.

Basically, C can be one of the best languages in the world provided you have a stupidly high amount of time. You can do anything you want, build operating systems, games, window managers, but it takes a lot of time.


This is why C made my experience of building a chatbot absolute hell. See, chatbots don't need to be that fast. They would be fine written in python, java, even something like PHP or Ruby.

Computers get faster, whereas code does not. It would make far more sense to have better, cleaner code written in a higher level language than bother maintaining complex C for something as simple as a bot.


Back on topic. The project has taught me a lot about C and its many libraries, and also forced me to search the internet for answers which has, in turn taught me more. I know that fgets is much safer than gets for getting input, and that it is also better than scanf due to not hiccuping on space characters. C is good at processing files themselves (through fgets) but not strings.

It has also made me realise how reliant we are on huge numbers of if selections if we do not take into account custom data structures such as lists and trees and recursive operations.

 

The project has also increased the amount of experience I have dealing with Visual Studio errors and warnings and how they affect my workflow.

Keywords: se1sa5

Posted by James E. Cleveland | 7 comment(s) | Share

March 15, 2009

I've been trying to remove a smaller string that i know is contained within a larger string (eg. string1<string2, where string1 is within string2). The following code I've produced returns a pointer to a string. The function requires inputs of, a main string (string2) and the substring to be removed (string1).

The following code produces no errors, but a simple warning suggesting that I use strncpy_s (because its safer on windows). The program will crash after it has compiled and it doesn't say why. Here is my code:

char* delSubStr(char *string,char *target)

{

int i, index;

int sublen = strlen(target);

int strilen = strlen(string);

char *pos, *temp;

temp = (char *)calloc(sizeof(temp)+1, sizeof(temp));

pos = strstr(string,target);

index = string - pos;

while (pos!=NULL)

{

strncpy(temp,string,index);

for (i =(index+1); i <=(strilen-1); i++)

{

temp[i] = string[i+sublen+1];

//strcpy((*string+i),*(string+i+1+sublen));

//string[i] = string[i+1+sublen];

}string = NULL;

}

return (temp);

 

Can anyone suggest a better method that prevents overlap in sub-string deletion and another string pointer being made temporarily? some alternative methods I have tried withint the for-loop have been commented out. If they look more promising just say. 

Keywords: deleting, removing sub-strings, strings, sub-strings

Posted by Richard P @ C/C++ for Beginners | 1 comment(s) | Share

March 07, 2009

Received from Scott Alexander:

 

heya, I've been having an issue with this bit of code

int FindKeywordInFile(){

int LoopCountOne;

int LoopCountTwo;

char Temp[KEYWORDLENGTH];

char Text[RESPONSELENGTH];

char Converter[1];

FILE *FilePointer;

FilePointer = fopen("JADEKEYWORDS.TXT","r");

 

 

if (FilePointer != NULL){while (fgets(Text,RESPONSELENGTH,FilePointer) !=NULL){

 

for (LoopCountOne = 0;LoopCountOne <= 16; LoopCountOne ++){

for (LoopCountTwo = 0;LoopCountTwo <= 20; LoopCountTwo ++){

*Converter = WordArray[LoopCountOne][LoopCountTwo];

strcat(Temp,Converter);

printf("\n%d.%c,%c,%s",LoopCountTwo,Converter[0],WordArray[LoopCountOne][LoopCountTwo],Temp);

}

strcpy(Temp," ");

}

}

 

}

return 0;

}

 

the array WordArray[17][21] is global, the problem seems to be with the line strcat(Temp,Converter);  If i /**/ it out the program works fine but with it in the program crashes as soon as it gets to it. Generaly the idea is that i've got a 2d array where keywords have been seperated out and now i need to put them into a single variable one at a time so I can match the input word with a keyword stored in a file, if anyone can see how I can fix this or has another way round please let me know.

Many Thanks

 

Scott Alexander

Posted by Andrew Harvey @ C/C++ for Beginners | 2 comment(s) | Share

February 11, 2009

Please find the C++ code from today's tutorial here. Please add comments to your code as your understand the lines. Please feel free to post questions to this forum if you have any difficulties.

Please note: as you have not covered strings in C++ yet, these have not been included in this example. I will update the files when strings have been covered, either in lectures or in tutorials. 

Using the class in your main function:

Read pages 9-11 (Term 2 Week 4 lecture 2)

Instantiate some objects with students (they do not have names yet, as strings are not used). Name the object after the student, then use the methods of the class to change some details. Then print the values of those objects.

For example:

sseStudent bob;

bob.setDegree(3);   

/*using a code to represent the degree course - we haven't actually specified what code is for what course yet...*/

//we can use comments like this in C++

bob.setApplied(true);

cout << "Bob is on degree program " << bob.getDegree() << "." << endl;

if(bob.isApplied())

{

    cout << "Bob is on an applied course." << endl;

}

Please ask for help if you need it. Feel free to expand the class or create your own examples of objects/classes - I'm always appreciative of good ideas! 

Posted by Andrew Harvey @ C/C++ for Beginners | 0 comment(s) | Share

January 26, 2009

http://redgloo.sse.reading.ac.uk/c4beginners/files/-1/483/main.c

Here are some examples of pointers used in the tutorials last week. Please ask any questions you need to on this board.

 Andrew

Posted by Andrew Harvey @ C/C++ for Beginners | 10 comment(s) | Share

December 23, 2008

In addition to the exercises Giuseppe has provided on Blackboard, I have compiled a set of exercises designed to improve basic programming skills. These are aimed at beginner level. Therefore, attempt these knowing that everyone should be able to at least partially complete the harder tasks on Blackboard.

http://redgloo.sse.reading.ac.uk/c4beginners/files/-1/475/Christmas+08+beginners+exercises.pdf


If these exercises prove to be useful, please let us know (under the post on RedGloo) and more will follow. If you have difficulty with any of the exercises, either from this list or from Blackboard, feel free to post your questions in this forum or hold your questions until the tutorials in the first week of term. If you have an answer that you would like someone to look at, you can e-mail me, attaching the .c file(s) (no .exe or others please) and I will aim to check them over and provide comments where necessary. Please remember that they are open to interpretation; if you feel you have completed the aims of the exercise and the program runs well, then you have probably succeeded. There will always be more than way to code the solutions and some will be better than others.

Thanks to Emma and Phil for their help.

Keywords: beginners, Beginners exercises, c, programming, SE1SA5

Posted by Andrew Harvey @ C/C++ for Beginners | 0 comment(s) | Share

October 24, 2008

I was talking about collecting together various online resources to coincide with the book lists we get for our courses and somebody suggested I post the few links I had for C/C++ here, so here you go;

http://www.freeprogrammingresources.com/cppbooks.html

http://www.freeprogrammingresources.com/ctutor.html

Contains a bunch of online books and tutorials on the matter.

So yeah, post your own links if you know of any good ones.

Posted by Thomas Rea @ C/C++ for Beginners | 0 comment(s) | Share

October 23, 2008

http://projecteuler.net/

 

Check it out - Suitable for beginners and advanced coders.

Posted by Andrew Harvey @ C/C++ for Beginners | 0 comment(s) | Share

Seeing as you can replace almost anything using #define, I decided to see exactly what I could achieve with this.

What did I get? No less than a version of C in the pinnacle of human dialect...

chav.h

chav.c

Keywords: c, chav, constants, define, preprocessor

Posted by James E. Cleveland | 7 comment(s) | Share

My suggestion for a first program would be something that utilises the formatting property of printf, and also declarations. For instance, declare a char array object[], and then print both "hello, %s!", object and "goodbye, %s!", object. Thus utilising these bits of knowledge and also normalising your string usage (you only have to specify "world" once).

 Attached is a sample source file.

 hello.c

Keywords: array, char, declaration, format, hello, printf, SE1SA5, world

Posted by James E. Cleveland | 0 comment(s) | Share

<< Back
/