• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drumms Useful Functions!
#1
As I've been programming I've noticed there is a nice collection of functions I've had to repeatedly use. So I thought I'd get some of them to you all. Maybe I could create a header file at some point..

1. Remove Newlines from given char pointer/array
Once you've finished with the string returned from this free it with free(returnedstring);
This string is useful when you may have taken user input, very often it will take all the chars (including the enter aka newline) If you don't want this bit of data and would rather deal with it, you can use this to go through every character until you reach the newline, and change that to 0, which in C is the end of a string.
[code=c]
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char* clean( char *cleanme ){
char* newstring = (char*) malloc (strlen(cleanme) + 1);
int i;

for (i = 0; *cleanme != '\n' ; i++, *cleanme++)
newstring[i] = *cleanme;

newstring[i] = '\0';
return newstring;
}[/code]
2. Random IP Generator
I used this as part of an IP source spoofer I was writing, I thought I might as well stick it up here, it shows how powerful sprintf can be. Of course again, you'll want to free the variable once you've finished with it, to avoid a memory leak.
[code=c]
char* randomIP(){

char* buffer = (char*) malloc (15 + 1); // +1 for NULL Char

memset (buffer, 0, 15+1);

int numb[4], i;

for (i = 0; i < 4; i++)

numb[i] = rand() % 255;

sprintf(buffer, "%d.%d.%d.%d\0", numb[0], numb[1], numb[2], numb[3]);

return buffer;

}
[/code]
3. Examine String Data Function
I found myself in need of some code for examining strings, and printing them to output in various different ways. This is great when you're working with data, and start seeing problems with your strings coming up, in the console, you'll often see a strange character, or none at all, because it's how the character works, this will let you view the decimal number of the characters (I'll add in hex and oct when I need it later) so you can examine the string on a much deeper level. This can be used when it may not be convenient to use GDB to examine the data (Which I recommend a go at)
[code=c]
#include <stdio.h>
#include <string.h>

void exdata(char *string, int mode){
int x, len = strlen(string);
if (mode == 1){ // Print Characters and the rest in dec
for (x = 0; x < len; x++){
if (isalnum(string[x]) )
putchar(string[x]);
else
printf("%d", string[x]);
}
}
if (mode == 2){ // Print all chars in decimal
for(x = 0; x < len; x++)
printf("%d", string[x]);
}

}
[/code]
More to come.
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply
#2
Updated with new function (No. 3)
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply
#3
They say necessity is the mother of invention. How about a description of circumstances which require the use of these FUNctions, Drumm? You can leave out the need for an IP generator as that one is obvious.

As far as I can tell, there are only 3 coders here. Maybe incite into the inception of the idea for each FUNction will inspire others to pursue the godlike power of computer programming. You would be, in effect, creating new coders by instilling the spark of curiosity, wonder, and possibility. Is the ground fertile to receive seed? What is life without personal creation?
Having long hair is great until you have to pull a footlong out of the dog's butt. flatank.blogspot.com
I. AM. LATCH.
  Reply
#4
3 Programmers, Is that yourself, pack3t and I? Perhaps adding a scenario would be useful for SEO which I'm sure you admins love.
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply
#5
I love SEO.
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [C++] Inline Functions Tutorial Clones 1 9,468 11-09-2012, 11:30 AM
Last Post: annaharris
  Two functions for the ELU Rating system Drumm 8 24,316 02-03-2011, 09:57 AM
Last Post: Drumm

Forum Jump: