Originally Posted by
Okiura
Hi guys, i need some help with C functions, i'm looking for a way to do two things but i'm struggling a little, was wondering if someone here can point me in the right direction.
1st problem:
This is a trivial one i want to join some variables together to make a new one. i know in vb.net it would be somthing along the lines of:
Code:
value3 = value1 & value2
so if value1 was "Hello" and value2 was "World" i then set a label.text to value3 it would read "HelloWorld", can i do this in C some how?
No. Sorry, but it's that simple, you can't do that.
C is a language of numbers. It is only trivially aware of strings and text. If you want to manipulate lines (or pages) of text you need to use functions like those in the standard C string library. You would do well to familiarize yourself with these functions early on as you will likely spend most of your time working with them. The example above would be accomplished with the strcpy() and strcat() functions, like this...
Code:
char words[24];
char second[12];
// put text in char arrays
strcpy(words,"Hello ");
strcpy(second, "World!");
// join them
strcat(words, second);
printf("%s\n",words);
Yeah I know,that sucks ... but that's how it's done.
2nd problem:
This the code for my test app:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int node1 = 1;
int node2 = 1;
int node3 = 1;
int node4 = 1;
while (node4 <= 26){
if (node1 <= 26){
printf("%i:%i:%i:%i\n", node1, node2, node3, node4);
node1 = node1++;
}
else if (node2 < 26){
node1 = 1;
node2 = node2++;
}
else if (node3 < 26){
node1 = 1;
node2 = 1;
node3 = node3++;
}
else{
node1 = 1;
node2 = 1;
node3 = 1;
node4 = node4++;
}
}
return 0;
}
what i'm looking to do is output a string from "AAAA" to "ZZZZ", i have it working for "1:1:1:1" to "26:26:26:26" but am i able to change these numbers in to letters by doing something like this:
Code:
int letnum = 10;
char alphab = "ABCDEFGHIJLMNOPQRSTUVWXYZ";
/* then do somthing like the following */
char letter = left(alpha, letnum);
/* to set "letter" to "J" */
Sorry if these are stupid questions, still very new to C.
Okiura
What you're looking for is the character's offset into the ascii code ... if you do this...
Code:
printf("%c %c %c %c", node1 + 'A', node2 + 'A', node3 + 'A', node4 + 'A');
You'll get characters instead of numbers on your screen...