Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Last active July 29, 2016 12:58
Show Gist options
  • Save manashmandal/73932f11ca25a7af9a7ddbbf0ede669b to your computer and use it in GitHub Desktop.
Save manashmandal/73932f11ca25a7af9a7ddbbf0ede669b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "cs50.h"
#define S_BUFFER 20
#define L_BUFFER 900
int words(const char sentence[ ])
{
int counted = 0; // result
// state:
const char* it = sentence;
int inword = 0;
do switch(*it) {
case '\0':
case ' ': case '\t': case '\n': case '\r': // TODO others?
if (inword) { inword = 0; counted++; }
break;
default: inword = 1;
} while(*it++);
return counted;
}
void reverse(char in[])
{
int i;
int j;
char out[strlen(in)];
for (i = 0, j = strlen(in) - 1; i < strlen(in); i++, j--){
out[i] = in[j];
}
out[i] = '\0';
strcpy(in, out);
}
void split(char input[]){
char end_delimiter[] = {' ', '\0'};
strcat(input, end_delimiter);
char wordArray[words(input)][L_BUFFER];
int wordCount = 0;
int i;
int j;
char temp[S_BUFFER];
for (i = 0, j = 0; i < strlen(input); i++){
if (input[i] == ' ' || ispunct(input[i])){
temp[j] = '\0';
reverse(temp);
if (ispunct(input[i])) {
char fucked[] = {input[i], '\0'};
strcat(temp, fucked);
i++;
}
//Copy to matrix here
strcpy(wordArray[wordCount], temp);
wordCount++;
j = 0;
strcpy(temp, "");
} else {
temp[j] = input[i];
j++;
}
}
int index = 0;
for (index = 0; index < wordCount; index++){
puts(wordArray[index]);
}
}
int main(void)
{
string in = GetString();
char content[L_BUFFER];
strcpy(content, in);
split(content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment