I need a way to print(and later on compare - but for now just print) characters 2 by 2 as in:
I have a string of 1F190307091810 and I have to print out:
1F 19 03 04 09 18 10.
I need this for Baudot code(I have segments of code given to me and I have to compare letters and numbers - together, two by two)

If you don't understand something please ask(I know I explain this in a weird way).

Member Avatar for deletedaccount

When you store it into a variable, make it so it's like:

string var1 = "1F 19 03 04 09 18 10"

And then print it. Or, do something like: printf("1F 19 03 04 09 18 10") or printf(var1) and it will be like it is in the variable.

Sorry I did the variable setting in C++. Just make a string in C and use same concept for storing.

Member Avatar for deletedaccount

Or you could try using a array and have the items for amount of characters and print first 2 and then do " " and then the next two and so on. That would work as well.

If you need to print more and more strings, the most efficient way in my opinion is to make a function with a parameter for the text to be printed two by two. Then, in that function, make a array and organize the characters so 1 char from the string is in it. Then, print out 2 characters, then " " and then another two until the string is complete. That way you could call the function and submit any string, and have it automatically printed out the way you want it.

Sorry I couldn't provide a code :(
At the moment, C for Windows development isn't my best language.

declaration of character arrays in C is like this:

char var1[] = "1F 19 03 04 09 18 10"

Or if you want a pointer instead of an array

char* var1 = "1F 19 03 04 09 18 10"

There are several ways to convert "1F190304091810" to "1F 19 03 04 09 18 10"
Since you have to later compare the 2-byte values, you might want to map them into an array

char values[7][3];

values[0] = "1F"
values[1] = "19"

To do that you will need a loop and a pointer to the original array, then copy bytes 2 at a time within the loop.

char original_char_array[] = "1F190304091810";
char* ptr = original_char_array;
for(i = 0; i < 7; i++)
   values[i][0] = *ptr++;
   values[i][1] = *ptr++;
   values[i][0] = '\0';
commented: I find it the most viable solution. +0

The issue is that I don't need to have same number of pairs,but I will have even number of characters - because 2 bytes make a letter or a number, so on that note can I use strlen to measure the size of string and divide it by 2 to get the number of paired elements ?
The other problem that I'm having atm is that I have to use dynamically allocated memory for each string(which I did) but I'm lost when I need to extract the elements 2by2.
If you want I can link code here.
Thanks for your help.

And yeah, just to say one more thing. I need to keep an infinite loop with one condition. When I type in "Stop Bruce Li" the program should exit( I can't make an infinite loop with this condition - whenever I enter second string after the first one my program crashes).

The issue is that I don't need to have same number of pairs,

Not really a problem. In my previous example if there are only 5 pairs then make the other 2 empty NULL strings.

   values[5][0] = '\0';
   values[6][0] = '\0';

'm lost when I need to extract the elements 2by2.

It doesn't matter if you dynamically allocate the memory for the original strings or not, extracting 2b2 is the same as what I posted before.

I haven't exactly grasped what are you looking for but I second AncientDragon.

You could also store your string 2b2 in a stack or , but you require to use structs and could be a little tricky to extract a pair in it.

Tyranneo, google Baudot code. I have to make a code that will read ciphered message and decipher it. In Baudot's code it functions in 2b2s where combination of 2 letters of numbers mean some letter in alphabet or number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.