awesome, I took out the
Thank you so much.Code:else puts ("NULL token");
awesome, I took out the
Thank you so much.Code:else puts ("NULL token");
Here is my full code. The red area is where the tokenized strings should be checked for comparability by the first character. Giving me same problem.
THe last thing i need and am trying to figure out is the strncmp to get it to put
hello world, a cool thing
into
a
cool
hello
thing
world
Code:#include <stdio.h> #include <string.h> void RecursiveReverse(const char *const sPtr); int main() { /*Part One*/ char buffer1 [] = {'T','h','i','s',' ','i','s',' ','a',' ','l','o','o','p','\0'}; char buffer2 [] = {"This is the second buffer"}; char buffer3 [80]; char buffer4 [100]; char buffer5 [100]; char *tokenptr; int i; printf("print a line:"); gets(buffer3); printf("\n%s\n", buffer1); printf("%s\n", buffer2); /*Part 2*/ char *pb; pb = buffer3; while(*pb != '\0'){ printf("%c", *pb); pb++; } printf("\n"); /*Part Three*/ printf("The line to print:\n"); gets(buffer4); printf("The new line is:\n"); RecursiveReverse(buffer4); printf("\n"); /*Part 4*/ printf("Please enter the string to be parsed:\n"); gets(buffer5); printf("\n"); char *b[20]; b[0] = strtok(buffer5, " ,;."); for(i = 0; i < 20; i++){ b[i+1] = strtok(NULL, " ,;."); if (b[i]) printf("%s\n", b[i]); //else puts ("NULL token"); } //for(i = 0; i < 2; i++){ //(strncmp(b[i], b[i+1], 1)) //printf("%s\n", b[i]); //} return 0; } void RecursiveReverse(const char *const sPtr) { if (sPtr[0] == '\0'){ return; } } else{ RecursiveReverse(&sPtr[1]); putchar(sPtr[0]); } }
You need to keep a count of how many actual tokens you get. After the first time strtok() returns NULL, it only keep only doing so -- there's clue #1.
If you break out of the for() loop at that point (via the "else"), i will equal the number of tokens (ie, one more than the last non-null index of b).
If you use a different variable to control the second loop, you can count upto the current value of i-1 doing the strcmp (or actually, i-2 since you want to compare the second last to the last).
Alternatively, you could use:
Code:i = 0; // reset while (b[i+1]) { // next element is still non-NULL [strcmp stuff] i++; }
Last edited by MK27; 02-24-2010 at 10:05 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
got it to work somewhat. It will post both just won't put in order and post repeats instead of the actual characters. won't post last character either....?
Thought blue might be needed for base case.
here is output:Code:#include <stdio.h> #include <string.h> void RecursiveReverse(const char *const sPtr); int main() { /*Part One*/ char buffer1 [] = {'T','h','i','s',' ','i','s',' ','a',' ','l','o','o','p','\0'}; char buffer2 [] = {"This is the second buffer"}; char buffer3 [80]; char buffer4 [100]; char buffer5 [100]; char *tokenptr; int i; printf("print a line:"); gets(buffer3); printf("\n%s\n", buffer1); printf("%s\n", buffer2); /*Part 2*/ char *pb; pb = buffer3; while(*pb != '\0'){ printf("%c", *pb); pb++; } printf("\n"); /*Part Three*/ printf("The line to print:\n"); gets(buffer4); printf("The new line is:\n"); RecursiveReverse(buffer4); printf("\n"); /*Part 4*/ printf("Please enter the string to be parsed:\n"); gets(buffer5); printf("\n"); char *b[20]; b[0] = strtok(buffer5, " ,;."); for(i = 0; i < 20; i++){ b[i+1] = strtok(NULL, " ,;."); if (b[i]) printf("%s\n", b[i]); else puts ("NULL TOKEN"); } i = 0; //if(strcmp(b[i], b[i+1])==0) // printf("%s\n", b[i]); while(b[i+1]){ if(strcmp(b[i], b[i+1]) == 0) printf("%s\n", b[i]); else if(strcmp(b[i], b[i+1]) < 0) printf("%s\n", b[i]); else if(strcmp(b[i], b[i+1]) > 0) printf("%s\n", b[i+1]); i++; } return 0; } void RecursiveReverse(const char *const sPtr) { if (sPtr[0] == '\0'){ return; } } else{ RecursiveReverse(&sPtr[1]); putchar(sPtr[0]); } }
Code:sol:~>a.out print a line:ok This is a loop This is the second buffer ok The line to print: okay The new line is: yako Please enter the string to be parsed: b.a.c b a c NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN NULL TOKEN a a
Last edited by patso; 02-24-2010 at 11:09 AM. Reason: show the code outputted
I'm not sure what you want to do there. You should replace that "NULL TOKEN" bit with:
This will end the loop, since there is no point continuing after that (hint: so you could use a while() condition here instead of the for loop, too).Code:else break;
wrt, maybe it will help to look at it indended more conventionally:
You should just call strcmp() once and assign the return value to a variable and use that. Anyway, so you are saying if the strings match, print the first one. If they don't and i < i+1, print the first one. If they don't and i > i+1, print the second one.Code:while(b[i+1]) { if(strcmp(b[i], b[i+1]) == 0) printf("%s\n", b[i]); else if(strcmp(b[i], b[i+1]) < 0) printf("%s\n", b[i]); else if(strcmp(b[i], b[i+1]) > 0) printf("%s\n", b[i+1]); i++; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
i was just trying that to see if it would work.
i have been told that I could use a bubble sort in conjunction with strcmp to get it to sort alphabetically.
I don't know how to go about putting the strcmp into a bubble sort.
You can use any sorting algorithm in conjunction with strcmp.
This is selection sort (very close to bubble sort)
This was done impromptu, so if there's an error, just let me know if you can't fix it.Code:char temp[MAX]; //MAX = largest element of data in array for(i = 0; i < MAX; i++) { for(j = i+1; j < MAX; j++) { if((strcmp(str1, str2)) > 0) { //str1 > str2, so we need to swap strcpy(temp, str1); strcpy(str1, str2); strcpy(str2, temp); } } }
It works for the desired number 4. But if I go under that or over it gives me a segmentation fault. How could I get strlen in there when it is being tokenized to determine the amount of tokenized words and use that count in my swap?
Sample input:
this is a sample
Output:
the number of words is 4
a
is
sample
this
The orange text is easy to print out I just used it to see if the function would work. I will use a for loop later for the amount.
Code:/*Part 4*/ printf("Please enter the string to be parsed:\n"); gets(buffer5); printf("\n"); char *b[20]; b[0] = strtok(buffer5, " ,;."); for(i = 0; i < 20; i++){ b[i+1] = strtok(NULL, " ,;."); if (b[i]) printf("%s\n", b[i]); else break; } printf("\n\n"); char temp[20]; int j; for(i = 0; i < 4; i++) { for(j = i+1; j < 4; j++) { if((strcmp(b[i], b[i+1])) > 0) { //str1 > str2, so we need to swap strcpy(temp, b[i]); strcpy(b[i], b[i+1]); strcpy(b[i+1], temp); } } } printf("%s\n%s\n%s\n%s\n", b[0], b[1], b[2], b[3]);
as soon as I go over three words, it won't alphabetize them....
Code:printf("Please enter the string to be parsed:\n"); gets(buffer5); printf("\n"); char *b[20]; b[0] = strtok(buffer5, " ,;."); num = 0; for(i = 0; i < 20; i++){ b[i+1] = strtok(NULL, " ,;."); num++; /*Counts the number of tokenized strings*/ if (b[i]) printf("%s\n", b[i]); else break; } num = num - 1; printf("\n%d\n", num); char temp[20]; //MAX = largest element of data in array for(i = 0; i < num; i++) { for(j = i+1; j < num; j++) { if((strcmp(b[i], b[i+1])) > 0) { //str1 > str2, so we need to swap strcpy(temp, b[i]); strcpy(b[i], b[i+1]); strcpy(b[i+1], temp); } } } for(i = 0; i < num; i++){ printf("%s\n", b[i]); }
Your code is for bubble sort, and this is selection sort. Bubble sort compares only adjacent values, selection sort compares i and j indexed items, instead of i and i + 1. Bubble code might work here, but I've never tried it with this sort.
I didn't mention that, did I? Oops!
Try that. If you have memory issues, it will still crash after a few names.
Last edited by Adak; 02-25-2010 at 12:34 PM.
i don't know what you mean. I thought i was using bubble sort
this is the output
print a line:asdf
This is a loop
This is the second buffer
asdf
The line to print:
asdf
The new line is:
fdsa
Please enter the string to be parsed:
this,is.a.string.to ;;;;parse
this
is
a
string
to
parse
6
Displaying contents of *b[]this
is
a
string
to
parse
press enter to continue
is
is
parse
rse
s
to
Selection sort is bubble sort with a slight optimization. It doesn't compare adjacent values except the first time it enters the inner for loop. It is in the same "family" of sorters as bubble sort. Most people would call selection sort, bubble sort, since the bubble sort is has all the buzz.
So, no memory problems?
works great for single inputted letters, as soon as there is more it gets screwed up