c++ - How to "delete" words and vowels by moving specific character combinations to an empty array? -


i'm in intro c++ class , assignment supposed generate "variable name" user based on phrase enter. program supposed replace spaces underscores , remove words "is", "the", "to", etc, vowels. stuck on how remove specific characters , combination of characters. not allowed use strings , rather using cstring , and moving parts of inputted array final "variable name" array. how attempting right now, not working.

void remove(char name[21], char copy[21]){//"deletes" or moves filler words , vowels inputted phrase      (int = 0; < strlen(name); i++){         if (name[i] == 't'&& name[i + 1] == 'h'&& name[i + 2] == 'e'&& (name [i + 3] == '_' || name[i + 3] == '\0')){             (int j = 0; j < i; j++){                 copy[i] = name[j];             }             (int j = + 2; j < strlen(name); j++){                 copy[i] = name[j];             }         }          //and vowels:             else if (toupper(name[i]) == 'a' || toupper(name[i]) == 'e' || toupper(name[i]) == 'i' || toupper(name[i]) == 'o' || toupper(name[i]) == 'u'){             (int j = 0; j < i; j++){                 copy[i] = name[j];              }             (int j = i; j < strlen(name), j++){                 copy[i] = name[j];             }         }     } 

some ideas have using 1 of cstring library functions strcpy or keeping count of how many characters beyond "i" have go. also, i'm concerned i'm undoing i'm doing on word if there multiple filler words in input. hugely appreciated.

because homework, i'll try , explain how should tackling problem rather flat out give direct solution:

a simple c string copy this:

void mycstringcopy(char* src, char* dst){     int =0;   while((dst[i]=src[i])!='\0'){i++;} } 

a c-string (char*) array of chars. src[i] looking @ specific character in string. if loop through string assigning each character source destination copy c-string. (src[i]=dst[i])!='\0' line satisfy while loop until copy null (the terminator c-strings), , i++ in loop's body ensures keep moving through c-string.
make filtered string copy, you'd want add additional logic before copying. purposes, copy better written out this:

void mycstringcopy(char* src, char* dst){     int =0;   while(1){     if((dst[i]=src[i])!='\0')        break;     i++;   } } 

in case looking both letter , string matches, you'll want loop through each filter term , compare current point in string before proceeding.

if find match between source string , filter term you'll want "skip" forwards many characters source string -- means read point , write point become de-synchronized, , such you'll want store them separately.

to compare filter terms you'll want use strncmp(str1,str2,length) or strnicmp(str1,str2,length) (and family) depending on if want case sensitive or not; looks have case in-sensitive, second 1 one you'll want. (warning: strncmp & strnicmp not portable -- may have make own).

as replacing characters, i'd separately replacing , removing different operations, if @ how copy function works should able figure out how replace 1 character in string another.

if you're stuck, here solution in online compiler. i'd advise solve on own, however.


Comments