C programming
#include <string.h> void StoreInputToChar2DArray(char str[], char args[][100], char seperator) { int startIndexCopy = 0; // Starting index of str to start copying string into args[] int indexToCopyTo = 0; // Which index of args[] to copy to int lastIndex = 0; // Know when to start copying string for each space sepearted strings int charFound = 0; int lastWasSpace = 0; // Begin processing user input for(int i = 0; i < strlen(str)+1; i++) { // Copy everytime a char is found if(str[i] == seperator) { if(indexToCopyTo == 0) { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex); strcat(args[indexToCopyTo], "\0"); // Remove spaces before and after string int lastCharIndex; for(int j = strlen(args[indexToCopyTo])-1; j >= 0; j--) { if(args[indexToCopyTo][j] == ' ') { args[indexToCopyTo][j] = '\0'; } else { lastCharIndex = j; break; } } int removeSpaceSpot = 0; int endStartSpace = 0; for(int j = 0; j < lastCharIndex+1; j++) { if(args[indexToCopyTo][j] != ' ') { endStartSpace = 1; } if(endStartSpace == 1) { args[indexToCopyTo][removeSpaceSpot] = args[indexToCopyTo][j]; removeSpaceSpot++; } if(j == lastCharIndex) { args[indexToCopyTo][removeSpaceSpot] = '\0'; } } } else { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex-1); strcat(args[indexToCopyTo], "\0"); // Remove spaces before and after string int lastCharIndex; for(int j = strlen(args[indexToCopyTo])-1; j >= 0; j--) { if(args[indexToCopyTo][j] == ' ') { args[indexToCopyTo][j] = '\0'; } else { lastCharIndex = j; break; } } int removeSpaceSpot = 0; int endStartSpace = 0; for(int j = 0; j < lastCharIndex+1; j++) { if(args[indexToCopyTo][j] != ' ') { endStartSpace = 1; } if(endStartSpace == 1) { args[indexToCopyTo][removeSpaceSpot] = args[indexToCopyTo][j]; removeSpaceSpot++; } if(j == lastCharIndex) { args[indexToCopyTo][removeSpaceSpot] = '\0'; } } } indexToCopyTo++; startIndexCopy = i+1; lastIndex = i; charFound = 1; } // Also copy the last string if(str[i] == '\0') { if(charFound == 1) { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex-1); strcat(args[indexToCopyTo], "\0"); // Remove spaces before and after string int lastCharIndex; for(int j = strlen(args[indexToCopyTo])-1; j >= 0; j--) { if(args[indexToCopyTo][j] == ' ') { args[indexToCopyTo][j] = '\0'; } else { lastCharIndex = j; break; } } int removeSpaceSpot = 0; int endStartSpace = 0; for(int j = 0; j < lastCharIndex+1; j++) { if(args[indexToCopyTo][j] != ' ') { endStartSpace = 1; } if(endStartSpace == 1) { args[indexToCopyTo][removeSpaceSpot] = args[indexToCopyTo][j]; removeSpaceSpot++; } if(j == lastCharIndex) { args[indexToCopyTo][removeSpaceSpot] = '\0'; } } } else { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex); strcat(args[indexToCopyTo], "\0"); // Remove spaces before and after string int lastCharIndex; for(int j = strlen(args[indexToCopyTo])-1; j >= 0; j--) { if(args[indexToCopyTo][j] == ' ') { args[indexToCopyTo][j] = '\0'; } else { lastCharIndex = j; break; } } int removeSpaceSpot = 0; int endStartSpace = 0; for(int j = 0; j < lastCharIndex+1; j++) { if(args[indexToCopyTo][j] != ' ') { endStartSpace = 1; } if(endStartSpace == 1) { args[indexToCopyTo][removeSpaceSpot] = args[indexToCopyTo][j]; removeSpaceSpot++; } if(j == lastCharIndex) { args[indexToCopyTo][removeSpaceSpot] = '\0'; } } } indexToCopyTo++; startIndexCopy = i+1; lastIndex = i; } } // Put "\0" as the last element to know when to stop searching array strcpy(args[indexToCopyTo],"\0"); return; } void StoreInputToChar2DArray2(char str[], char args[][100], char seperator) { int startIndexCopy = 0; // Starting index of str to start copying string into args[] int indexToCopyTo = 0; // Which index of args[] to copy to int lastIndex = 0; // Know when to start copying string for each space sepearted strings int spaceFound = 0; // Begin processing user input for(int i = 0; i < strlen(str)+1; i++) { // Copy everytime a char is found if(str[i] == seperator && str[i+1] != ' ') { if(indexToCopyTo == 0) { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex); strcat(args[indexToCopyTo], "\0"); indexToCopyTo++; spaceFound = 1; } else { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex-1); strcat(args[indexToCopyTo], "\0"); indexToCopyTo++; spaceFound = 1; } startIndexCopy = i+1; lastIndex = i; } if(str[i] == seperator && str[i+1] == ' ') { lastIndex += 1; } // Also copy the last string if(str[i] == '\0') { if(spaceFound == 1) { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex-1); strcat(args[indexToCopyTo], "\0"); indexToCopyTo++; } else { strncpy(args[indexToCopyTo], str+startIndexCopy, i-lastIndex); strcat(args[indexToCopyTo], "\0"); indexToCopyTo++; } startIndexCopy = i+1; lastIndex = i; } } // Put "/0" as the last element to know when to stop searching array strcpy(args[indexToCopyTo], "\0"); return; } void ClearCharArrays(char args[][100], char* args2[], char str[], int strSize) { for (int i = 0; i < 100; i++) { memset(args[i], 0, sizeof(args[i])); } memset(str, 0, sizeof(char) * strSize); for (int i = 0; i < 100; i++) { args2[i] = '\0'; } return; }