#how do I use 'strcat', but in reverse

42 messages · Page 1 of 1 (latest)

frail quarryBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

kind gale
#

how do I use 'strcat', but in reverse

noble geyser
#

You need to do it manually.

kind gale
#

How would I?

mint ginkgo
#

Hmmm, itoa() is not a standard C library function.

Yet, it's the only function I can find that can convert to other bases.
Having said that, converting to base-26 but only going as far as [0-9] is no different to just converting to base-10 surely?

If you wanted numbers [0-9] to be converted to ['0'-'9'], then just add '0' to i.

kind gale
#

ive changed to

itoa(int1[i],chara1[i],10);

#

not sure what you meant about 'then just add '0' to i'

agile sphinx
#

You can just reverse the chara1 string after for loop and you'll get the same result without worrying about strcat

kind gale
#

how would i change the end result from

0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210

to

0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789

?

agile sphinx
#

Since you're using non-standard functions, you could use strrev to reverse the string

kind gale
#

ah i see thanks

frail quarryBOT
#

@kind gale Has your question been resolved? If so, type !solved :)

kind gale
#

SHINKU HADOKEN!!!!

the final version:


    int int1[10];
    char chara1[10][10];
    char chara2[10][10];
    //int1[0] = 0;

    for(i=0;i<10;i++){

        int1[i] = i;
        itoa(int1[i],chara1[i],10);

        strcat(chara1[i],chara1[i-1]);
        strcpy(chara2[i],chara1[i]);
        strrev(chara2[i]);




        printf("%s\n",chara2[i]);


    }```

which outputs:

0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
kind gale
#

=]

flint ocean
#

Your program still contains undefined behavior and is therefore an invalid program as per the C standard.

neon scroll
#

welp

#

i guess

buoyant gull
# kind gale not sure what you meant about 'then just add '0' to i'

By adding the character zero, so '0', to an integer i that ranges from 0 to 9, you can easily get the characters '0' to '9'. In this specific case you can just make the for-loop go from the character '0' to '9' directly though, of course, since chars are also just integers.

#

;compile -Wall -Wextra -Wpedantic -Werror -Wfatal-errors -fsanitize=address,undefined -g

#include <stdio.h>

int main(void) {
    for (int i = 0; i < 10; i++) {
        putchar(i + '0');
    }
    putchar('\n');
}
timber chasmBOT
#
Program Output
0123456789
neon scroll
#

;kompai

#

;kompail

kind gale
mint ginkgo
#

Okay.

#include <stdio.h>

int main(void) {
    char arr[10] = {0};
    for (int i = 0; i < 10; i++) {
        arr[i] = i + '0';
    }
    puts(arr);
}

(credit to @buoyant gull )

#

You should be able to take this snippet and apply it to your program, yes?

kind gale
#

ye

    for (int i = 0; i < 10; i++) {
        arr[i] = i + '0';
        puts(arr);
    }```
#

this is actually the next exercise lol

buoyant gull
#

So do [11] to make space for the null terminator at the end of the string @kind gale

mint ginkgo
#

True, dat.
Good spot.

buoyant gull
#

That's why I chose to use putchar(), cause it's just easier

kind gale
#

So, the function strcat() puts a char behind a string,
Is there a function that puts a char in front of a string

buoyant gull
kind gale
#

wow

#

strrev(strcpy(chara2[i],chara1[i]));

actually works lol

neon scroll
#

wat

#

what is ctrcpy and strrev here

buoyant gull
#

;compile -Wall -Wextra -Werror -Wpedantic -Wshadow -Werror -fsanitize=address,undefined -g

#include <stdio.h>
#include <string.h>

char *strrev(char *s) {
  if (!s || !*s) {
    return s;
  }

  size_t i = 0;
  size_t j = strlen(s) - 1;

  while (j > i) {
    char c = s[i];
    s[i] = s[j];
    s[j] = c;

    i++;
    j--;
  }

  return s;
}

int main() {
  char s1[] = "";
  printf("%s\n", strrev(s1));
  char s2[] = "a";
  printf("%s\n", strrev(s2));
  char s3[] = "ab";
  printf("%s\n", strrev(s3));
  char s4[] = "abc";
  printf("%s\n", strrev(s4));
}
timber chasmBOT
#
Program Output
a
ba
cba
kind gale
#

!solved

frail quarryBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity