How can you convert a program written in C (using ASCII) so that it can handle Unicode strings?
I realize there is no simple answer to this question, so I'm posting the source code from two short programs (from Dave Mark's book on C) that I want to convert to handle Unicode.
How can you replace the ASCII code with Unicode/UTF-8 code?
Program 1: (Program to print ASCII characters. I want to extend this to print some Unicode characters.)
Program 2: (Program to count the number of words typed into the program. I want to replace the ASCII code with Unicode/UTF-8.)
Thanks!
I realize there is no simple answer to this question, so I'm posting the source code from two short programs (from Dave Mark's book on C) that I want to convert to handle Unicode.
How can you replace the ASCII code with Unicode/UTF-8 code?
Program 1: (Program to print ASCII characters. I want to extend this to print some Unicode characters.)
Code:
#include <stdio.h>
void PrintChars( char low, char high );
int main (int argc, const char * argv[]) {
PrintChars( 32, 47 );
PrintChars( 48, 57 );
PrintChars( 58, 64 );
PrintChars( 65, 90 );
PrintChars( 91, 96 );
PrintChars( 97, 122 );
PrintChars( 123, 126 );
return 0;
}
void PrintChars( char low, char high ) {
char c;
printf( "%d to %d ---> ", low, high );
for ( c = low; c <= high; c++ )
printf( "%c", c );
printf( "\n" );
}
Program 2: (Program to count the number of words typed into the program. I want to replace the ASCII code with Unicode/UTF-8.)
Code:
#include <stdio.h>
#include <ctype.h> //This is to bring in the declaration of isspace()
#include <stdbool.h>
#define kMaxLineLength 200
#define kZeroByte 0
void ReadLine( char *line );
int CountWords( char *line );
int main (int argc, const char * argv[]) {
char line[ kMaxLineLength ];
int numWords;
printf( "Type a line of text, please:\n" );
ReadLine( line );
numWords = CountWords( line );
printf( "\n---- This line has %d word", numWords );
if ( numWords != 1 )
printf( "s" );
printf( " ----\n%s\n", line );
return 0;
}
void ReadLine( char *line ) {
int numCharsRead = 0;
while ( (*line = getchar()) != '\n' ) {
line++;
if ( ++numCharsRead >= kMaxLineLength-1 )
break;
}
*line = kZeroByte;
}
int CountWords( char *line ) {
int numWords, inWord;
numWords = 0;
inWord = false;
while ( *line != kZeroByte ) {
if ( ! isspace( *line ) ) {
if ( ! inWord ) {
numWords++;
inWord = true;
}
}
else
inWord = false;
line++;
}
return numWords;
}
Thanks!