score:0
utf-8 is a variable-length encoding, so some characters take only one byte while others take several.
if you are processing the string character-by-character using a switch
statement, then you should probably use a wide-character string instead:
#include <stddef.h>
wchar_t mytext[]= l"ητια ητιααα λουλουδιασμενη!!!1234567890";
a wide-character has type wchar_t
instead of char
, and is intended to be large enough to store any single character in the current locale. a wide-character string constant is prefixed with the l
character.
in your switch
statement you can use wide-character constants in your case expressions (which are also prefixed by the l
character):
switch (c)
{
case l'λ':
/* handle capital lambda */
break;
case l'α':
/* handle capital a */
break;
/* ... */
}
score:1
since utf-8 characters can have multiple bytes, and strlen just counts the number of bytes until the first null character, strlen will overcount the length of utf-8 strings. one solution is to use mbstowcs() to convert the string to a wide character string, then wcslen() to get the length of the wide-character string.
p.s. here is a demonstration of the effect mentioned in the question.
Source: stackoverflow.com
Related Query
- English and Greek UTF characters difference
- What is the difference between javac and the Eclipse compiler?
- What is the difference between the Eclipse Package Explorer and the Eclipse Project Explorer?
- What is the difference between Eclipse for Java (EE) Developers and Eclipse Classic?
- Java Eclipse: Difference between exporting as a JAR and exporting as a Runnable JAR
- Difference between Ctrl+Shift+F and Ctrl+I in Eclipse
- What is the difference between Android 4.4W (API20) and Android L (API20, L Preview) in the SDK Manager?
- What is the difference between Eclipse 3.7 and Eclipse 4.1?
- In Eclipse, what is the difference between modulepath and classpath?
- What is the difference between MinGW, MinGW-w64 and MinGW-builds?
- Difference between intellij Project make and Maven Compile?
- What is the difference between rollback, backout and strip in the Mercurial Eclipse plugin?
- What is the difference between call hierarchy and find references eclipse?
- What's the difference between program arguments and VM arguments?
- Eclipse: Difference between clean, build and publish
- difference between clean and clean working directory in Eclipse WTP with Tomcat
- What is the difference between Eclipse with Spring IDE plugin and Spring Tool Suite alone?
- Difference between Java working set and Resource working set in Eclipse
- What's the difference between "Push..." and "Push to upstream..." in Eclipse
- Eclipse: Difference between the "Tasks" view and the "Task List" View
- Difference between static and dynamic web project in eclipse
- What the difference between Eclipse 3.7, 3.8 and 4.2?
- Is there a key binding for Next Difference and Previous Difference in Eclipse Compare Editor?
- What's the difference between "Add JARs" and "Add External JARs" in Eclipse?
- Eclipse Compare Editor: difference between the 'next difference' and 'next change' buttons?
- What is the difference between Eclipse and Netbeans if I want to use only the Java in it?
- difference between wtpwebapps and webapps folder in tomcat
- Difference between Eclipse's "clean project" and Maven's "mvn clean" in m2e
- Subversive: Difference between depth 'working copy' and 'recursive' when merging?
- What is the difference in ECore between containment and reference?
More Query from same tag
- XPATH - Can I retrieve the type of a variable with an XPATH query for eclipse-pmd plugin?
- Can't find installed jre page in Eclipse indigo IDE for C/C++ Developers
- Where does eclipse publish(disk location) the web project?
- Outliner for java files
- Prevent Zest 2.0 GraphViewer from executing the layout algorithm with refresh
- Android Screen Marign
- Eclipse Indexer with IAR plugin
- Eclipse formatter JUST indenting
- RCP exportWizard remove unnecessary items
- How to add JFreeChart in Eclipse Kepler?
- Reading excel file inside the user.dir
- First time JUnit user: Why eclipse always shows error (Red indicator)
- Netsuite SDF Deploy Error - Gateway Time-Out
- Not able to run Hadoop from eclipse saying Hadoop location in eclipse(Error:NULL)
- How can I select dropdown from Selenium POM when my constructor Select(By) is undefined?
- My SpringBoot project won't load into Eclipse
- iWnn ime crashes with EditText on Nexus7 2013 4.4.2
- Which version of Eclipse works with OS X 10.4.11?
- Unable to create 4.2.2 Android Emulator in the AVD
- subversion issue with eclipse
- Switching Background Images
- JOGL errors in eclipse
- jooq configuration input parameter on call sp
- Problems looping in a while loop with nested if statements
- how to integrate play framework with scala IDE
- Reusing images in same layout
- Eclipse context menue options are missing
- How to prevent Eclipse from auto-importing packages?
- Eclipse ClearCase plugin and CCRC
- Unable to reverse a String after converting it to String Array