![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#22 |
|
Programmer
|
Have to say, I was finished two classes on C++ (the second one was Data Structures) and I didn't fully understand Pointers until I was working with Linked Lists.
I do declare pointers as extremely useful! I HAVE SPOKEN! lol
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former. |
|
|
|
|
|
#23 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Your tailend contributions to recent threads are quite mind boggling :eek: .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#24 |
|
Programmer
|
LOL, yes yes, I am delayed with my posting.
I appologize for this, but I have been in school and working, so I don't have as much time to check in as I would like. Plus I just started getting to know these languages better, so I am trying to give opinional input as well as ask questions in the form of possible answers (not related to this post in general, but others) since this is one of the ways I learn. Plus that was kinda of a hidden suggestion to look into Linked Lists for a way to learn pointers.... don't really see how that is mind boggling unless you don't read between lines. ![]()
__________________
Only two things are infinite, the universe and human stupidity, and im not sure about the former. |
|
|
|
|
|
#25 | |||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Ka-ching!
Quote:
#include <stdio.h>
#include <stdlib.h>
/* returns how many words there are in the sentence */
int wordcount( char * str )
{
int s = 0;
char * p = str;
while(*p != '\0')
{
if(*p == ' ')
s++;
p++;
}
return ++s;
}
/* returns the current word len */
int wordlen( char str[] )
{
static int s = 0;
int len = 0;
if(str == NULL)
{
s = 0;
return 0;
}
while(str[s] != '\0')
{
if(str[s] == ' ')
{
s++;
return ++len;
}
s++;
len++;
}
return ++len;
}
// gives location of the letters, skips whitespace
int goLetter( char str[] )
{
static int i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ')
{
i++;
return -1;
}
return(i++);
}
return -1;
}
/* split words and put them in an array, and return that */
char** split ( char* str )
{
char **p;
int i, j , x = 0;
int len = wordcount(str);
int curword = 0;
/* allocate space for multi-dimensional array */
p = malloc(len * sizeof *p);
/* allocation gone wrong */
if(p == NULL)
exit(1);
/* allocate space for each item */
for(i = 0; i < len; i++)
p[i] = malloc(wordlen(str) * sizeof *p[i]);
/* I'm a cheater :D, restart */
wordlen(NULL);
/* initialize */
for (i = 0; i < len; i++)
{
curword = wordlen(str);
for (j = 0; j < curword; j++)
{
if((x = goLetter(str)) != -1)
*( *( p + i ) + j ) = str[x];
else
*( *( p + i ) + j ) = '\0';
}
}
return p;
}
/* entry */
int main( void )
{
char** array = split("abc 123 asdf foobar");
printf("Array[0] contains %s\n", array[0]);
printf("Array[1] contains %s\n", array[1]);
printf("Array[2] contains %s\n", array[2]);
printf("Array[3] contains %s\n", array[3]);
free(array);
return 0;
}![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|||
|
|
|
|
|
#26 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Don't forget "strtok"
.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#27 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
doesn't strtok only allow 1 type of delimeter? Writing you own i believe allows more freedom, no?
__________________
www.heldtogether.co.uk |
|
|
|
|
|
#28 | ||
|
Programmer
Join Date: Dec 2005
Posts: 65
Rep Power: 3
![]() |
Quote:
Quote:
I took a look at this function a a while ago, and it's just poor. Quoted from the man page for strtok:BUGS
Never use these functions. If you do, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
The identity of the delimiting character is lost.
The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you. |
||
|
|
|
|
|
#29 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#30 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
I'm not sure what other 'types' of delimiters you would like. strtok() isn't that bad..compared to gets() :p Even DaWei agrees gets() is bad ![]() Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|