how can make this code shorter?

Pages: 12
closed account (oz0i0pDG)
.
Last edited on
welcome!
Give us all the gory details, more is usually good as long as it is relevant.
can you use c++ strings?
can dictionary be a map or other lookup table instead of a hunt and seek?
can you say in words exactly what it needs to really do, instead of making us guess?
if this is c++ line 6 is illegal, arrays must be constant sized at compile time.
9-16 is weird. you avoid copying the 0 only to have a special snowflake branch to copy the zero. just copy the whole thing, including the 0. the whole loop would be 1 line then.
and on TOP of that, it appears to copy word2 into word2. it does nothing at all? Or it shortens by 1 letter, which you can simply do by word[strlen(word)-2] = 0; maybe it does cut a letter off but its a lot of work for it?
20-26 looks like it could be replaced with a 1 line memcpy
not sure what the last loop does, but likely it too can be cut up depending on what you really need.
all those for loops with all that recursion ... is it a wee bit slow?
Last edited on
closed account (oz0i0pDG)
thank you for replying , and yes I can use strings but Im trying to keep the code in this form all i wanna do is make it simpler,I just edited the code a bit can you tell me what I should replace line 6 with? and how I can simplify the code more thank you sm in advance
well, if you can use a string or a vector, line 6 needs to be some sort of container that can be sized at run time.
if you can't, the old school way is to make it too big for its needs, eg [1000] and it will work as long as the input is something that fits (if you care you can check the input, if too large, fuss at user and stop).
you can also allocate a pointer to fit, char*, and delete it after. If you know dynamic memory. This seems... risky ... with recursion if you don't know what you are doing with dynamic mem. **

make the changes I suggested or explain better what the chunks do if I got it wrong. Then post the code again in a new response, rather than edit the original. Its confusing to follow edited posts :) (he says, editing his posts).

** its not just the recursion but multiple return statements and general flow that make it tricksy to use a pointer here.
Last edited on
closed account (oz0i0pDG)
honestly im a bit lost can you show me how

how what?
char word2[1000]{0};
1
2
3
4
5
6
7
8
9
10
11

    for(int i = 0; i < length; i++){

        for(int i = 0; i < length; i++){
            if(i != length-1){
                word2[i] = word2[i];
            }
            else{
                word2[i] = '\0';
            }
        }


ALL that code is really just
word2[length-1] = 0;
or tell me what I missed.
Last edited on
closed account (oz0i0pDG)
20-26 looks like it could be replaced with a 1 line memcpy this
closed account (oz0i0pDG)
.
Last edited on
1
2
3
4
5
6
7
8
  int ind = 0;
        for(int c = 0; c < length; c++){
            if(c != i){
                word2[ind] = word[c];
                ind++;
            }
        }

you will have to toy with the indices but
memcpy(destination, source, # of bytes)
so
memcpy(word2, word, i); //I think?
it could also be length, not i, not sure.

it looks like the two for loops ended by this point, but the misaligned brackets make it really hard to tell. if that is true:
technically i has no valid value, because it should have been scoped to each loop (which is extra weird that you use i for both loops, but the outer one does nothing, so that is worth saying what you think it does..) anyway, i has no value here in strict c++. Its probably length, on old compilers that tolerate using it.

if its not true, I still don't know if its i or length or even c for the memcpy.

I don't mean to be mean or rude but its a little convoluted.
Last edited on
closed account (oz0i0pDG)
.
Last edited on
Not surprised. i may be totally wrong amount to copy. what exactly is that loop doing?

if it removes a letter, you need 2 copy statements.
if it copies the first part of word to word2, we just need to know how much it grabs.

you can clean it a little:
1
2
3
4
for (int c = 0, ind = 0; c<length; c++)
{
  if(c!=i)  word2[ind++]=word[c];
}


everything hinges on what 'i' is at this point
Last edited on
closed account (oz0i0pDG)
.
Last edited on
closed account (oz0i0pDG)
.
Last edited on
closed account (oz0i0pDG)
okay it worked
actually the {} are not needed either but I prefer not to have unblocked loops and conditions. you can remove if its sheer code bulk you want to cut, or put it all on one like
for()
{ if () { statement}}
sort of a lame style but if you want to cut bulk and don't mind it for one line (of real code) blocks...
and all the brackets are optional on that one


for (int c = 0, ind = 0; c<length; c++) if(c!=i) word2[ind++]=word[c];

if you prefer ugly over wordy, you can even shove the work into the last loop slot
for (int c = 0, ind = 0; c<length; ?: stuff here) ; //this style is unreadable and unwise.
Last edited on
closed account (oz0i0pDG)
.
Last edited on
closed account (oz0i0pDG)
.
Last edited on
closed account (oz0i0pDG)
A c-string (i.e. word) which contains the word to be tested if it is shrinkable or not. < first loop
closed account (oz0i0pDG)
can I use anything else than strcpy and strcmp and strlen?
standard string has its own length, you can just ask for it, it has an assignment operator, so you can just assign them. You can just compare them.

eg string s = "hello"; //same as strcpy(s, "hello");
you can append to them:
s+= "world"
and so much more ...


you can use all kinds of C tools, but they are crude... you said you wanted to use them..
strstr, sprintf, strcat, strtok are some of the most used C ones.
Last edited on
Pages: 12