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.
42 messages · Page 1 of 1 (latest)
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.
how do I use 'strcat', but in reverse
You need to do it manually.
How would I?
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.
ive changed to
itoa(int1[i],chara1[i],10);
not sure what you meant about 'then just add '0' to i'
You can just reverse the chara1 string after for loop and you'll get the same result without worrying about strcat
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
?
Since you're using non-standard functions, you could use strrev to reverse the string
ah i see thanks
@kind gale Has your question been resolved? If so, type !solved :)
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
=]
Your program still contains undefined behavior and is therefore an invalid program as per the C standard.
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');
}
0123456789
that's pretty good
but, the point of my exercise was to assign into arrays =]
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?
ye
for (int i = 0; i < 10; i++) {
arr[i] = i + '0';
puts(arr);
}```
this is actually the next exercise lol
Note that the array should actually be char arr[11] = {0}; if you want to print it with puts()
So do [11] to make space for the null terminator at the end of the string @kind gale
True, dat.
Good spot.
That's why I chose to use putchar(), cause it's just easier
So, the function strcat() puts a char behind a string,
Is there a function that puts a char in front of a string
No, you'll have to implement such a function yourself
Note that strcat puts a string behind another string, not just a char behind a string
strrev() is not part of any C standard, but here's the simple implementation:
https://stackoverflow.com/a/29408476/13279557
;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));
}
a
ba
cba
!solved
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