How to take a string and arrange words in alphabetical order using C -
i'm stuck. i've got quite big programming assignment , of looks easy, part i'm stuck on splitting string of text individual words in array , sorting them in alphabetical order.
e.g. if string contained following: "i stuck. please help me stack exchange" save words in array , output them in next order:
am exchange help me please stack stuck
could guys please help?
edit: here's have far:
#include <stdio.h> #include <stdlib.h> int main() { char str[] = "this test string, else? lol"; char *hhh; int i; for(i=0;str[i];i++){ str[i]=tolower(str[i]); //converts string lower case } //breaks string separate words based on spaces , //punctuation (anything signals end of word) hhh = strtok(str," ,.-:;?!"); while(hhh != null){ printf("%s \n",hhh); hhh = strtok(null, " ,.-:;?!"); } }
as can see i've converted words lower case r , can output them have no thought how sort them in alphabetical order. looked bubble sorting , understand don't understand how utilize accomplish need.
my code pretty "manual" meaning don't utilize stuff strtok
or tolower
. manually loop through myself. if don't that, replace corresponding parts functions. here go:
#include <stdio.h> #include <string.h> #define _wlen 32 // maximum length of each word #define _nwords 256 // maximum number of words in sentence void word_swap(char** w1, char** w2); int main(int argc, const char * argv[]) { char* sentence = "the quick brownish fox jumps on lazy dog."; // sentence char to_ignore[10] = ".,-\"'!?()"; // characters ignored char words[_nwords][_wlen]; // words stored here int i, j, k=0, l=0, word_count, swapped=0; // variables needed /* first loop through sentence separate words */ (i=0; i<_nwords*_wlen; i++) { /* if reach end of sentence, finish lastly word '\0' , quit loop */ if (*(sentence+i) == '\0') { words[k][l] = '\0'; word_count = k+1; break; } /* check if current character 1 want ignore. if so, skip next character. */ (j=0; j<10; j++) { if (to_ignore[j] == *(sentence+i)) goto end_for; } /* if current character not space, add together word */ if (*(sentence+i) != ' ') { words[k][l] = *(sentence+i); l++; } /* ... if space, finish current word '\0' , move next word */ else { words[k][l] = '\0'; k++; l=0; } end_for:; } /* convert lowercase it's easy sort words */ (i=0; i<word_count; i++) { (j=0; j<_wlen; j++) { if (words[i][j] == '\0') break; /* if letter uppercase (ascii codes 65-90) add together 32 it, lowercase variant */ if (words[i][j] >= 65 && words[i][j] <= 90) words[i][j] += 32; } } /* bubble sort words in alphabetical order */ { (i=0; i<word_count-1; i++) { if (strcmp(words[i], words[i+1]) > 0) { word_swap(&words[i], &words[i+1]); swapped = 1; break; } else swapped = 0; } } while (swapped != 0); /* print words on screen */ (i=0; i<word_count; i++) printf("%s\n", words[i]); } void word_swap(char** w1, char** w2) { char tmp[_wlen]; memcpy(&tmp, w1, _wlen); memcpy(w1, w2, _wlen); memcpy(w2, &tmp, _wlen); }
c
No comments:
Post a Comment