hey all again =]
I'm having problems with the putchar() function, here is the code:
error: invalid operands to binary + (have 'char *' and 'char *')|Code:putchar(chara1[i] + chara1[i-1]);
any help?
hey all again =]
I'm having problems with the putchar() function, here is the code:
error: invalid operands to binary + (have 'char *' and 'char *')|Code:putchar(chara1[i] + chara1[i-1]);
any help?
I'm not sure if its the right function to use, basically:
so, the function strcat() puts a char behind a string
is there a function that puts a char in front of a string
If chara1 is an array of strings, then you're trying to add two pointers, which doesn't make any sense (C doesn't support string concatenation using the "+" operator like some languages).
putchar writes a char to stdout. That doesn't seem like the function you want.
strcat concatenates a string to the end of another string. If you want to "put" a character in front of a string, you'll need to move all characters in the string forward and then set the first character to the new character:
Code:// untested // s is the string, c is the char // make sure the buffer holding the string s is long enough to hold another character before doing this: memmove(s + 1, s, strlen(s) + 1); // +1 for the terminator s[0] = c;
You have no error checking for the length of the string and the size of the array.
A better way of doing this, providing some error checking in the actual code:
After prepending the character to the string, the combined dest string could be copied back to the src array, using strcpy()Code:#include <stdio.h> int main(void) { char ch = 'A'; char src[64] = "This is a test."; char dest[128] = ""; sprintf(dest, "%c%s", ch, src); printf("Final: [%s]\n", dest); return 0; }
I consider this safer considering the level of experience of the O/P.
Last edited by rstanley; 4 Weeks Ago at 01:39 PM.
its maybe easier to show you what im doing =]
the current output is:
0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
which is correct
the problem is, I am trying to find a way to assign the array in the right order without having to use the strrev() function
any help?
Code:int i; int int1[10]; char chara1[11][11] = {0}; char chara2[10][10]; for(i=0;i<10;i++){ int1[i] = i; itoa(int1[i],chara1[i],10);//convert number from int to string strcat(chara1[i],chara1[i-1]);//Concatenates from previous array number strcpy(chara2[i],chara1[i]);//copying array otherwise it would get scrammbled strrev(chara2[i]);//reverses string because 'strcat(chara1[i],chara1[i-1]);' puts the char behind the string instead of infront printf("%s\n",chara2[i]); }
Last edited by WaterSerpentM; 4 Weeks Ago at 03:34 PM.
wow
actually works lolCode:strrev(strcpy(chara2[i],chara1[i]));
Yep, strcpy returns a pointer to the destination string, so it's the same as calling strcpy and strrev separately with the same destination (and there's no performance benefit to doing it one way or the other, maybe unless you have all compiler optimizations disabled or you're using a really old or bad compiler).