Thread: How to search for a degree symbol ``?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    61

    How to search for a degree symbol ``?

    Hi,

    I'm trying to terminate a string at the first instance of the degree symbol ``. But when I try:
    Code:
    if((ptr = strchr(data, '')) == NULL)
    ...
    I get the following error:
    Code:
    error: multi-character character constant [-Werror=multichar
    I tried, to use `strstr` in case it might help. It compiled cleanly, but never finds the desired character. I ran through each character, displaying the character and the decimal value. This must be the degree character:
    Code:
    �       194
    �       176
    Is there a simple way for me to find the position of the first degree symbol in a string? Or do I have crawl through the string, character by character with the following?:
    Code:
    while(ptr[0] != 194 && ptr[1] != 176)
        ptr++;
    Ty

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    746
    IF you are using a multibyte charset (UTF-8, for example) you should convert this string to wchar_t string and use wcsrchr() instead of strchr():

    Code:
    /* test.c */
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <wchar.h>
    #include <locale.h>
    
    int main( int argc, char *argv[] )
    {
      int n;
      mbstate_t ps;
      wchar_t *dest, *p;
      size_t size;
    
      if ( argc != 2 )
      {
        fputs( "Usage: test <string>\n", stderr );
        return EXIT_FAILURE;
      }
    
      // You MUST setup LC_CTYPE for mbsrtowcs() to work!
      setlocale( LC_CTYPE, "en_US.utf8" );
    
      // allocate space to wchar_t string.
      size = strlen( argv[1] );
      if ( ! ( dest = malloc( sizeof( wchar_t ) * (size + 1) ) ) )
      {
        fputs( "Cannot allocate space for widechar string.\n", stderr );
        return EXIT_FAILURE;
      }
    
      // Convert multibyte character string to wchar string.
      memset( &ps, 0, sizeof ps );
      mbsrtowcs( dest, (const char **)&argv[1], size, &ps );
    
      // Try to find '' (notice the L in the literal).
      if ( p = wcschr( dest, L'' ) )
        printf( "%p\n", p );  // print the pointer if found.
      else
        puts( "Not found" );
    
      free( dest );
    
      return EXIT_SUCCESS;
    }
    IF you are using ISO-8859-1 or WINDOWS-1252 encoding (single byte charset), strchr() will work just fine.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    441
    Quote Originally Posted by Yonut View Post
    Hi,
    Is there a simple way for me to find the position of the first degree symbol in a string? Or do I have crawl through the string, character by character with the following?:
    Code:
    while(ptr[0] != 194 && ptr[1] != 176)
        ptr++;
    Ty
    If using UTF-8, just create the degree symbol manually.

    Code:
    const char degreesymbol[3] = {194, 176, 0};
    
    firstdegree = strstr(searchstring, degreesymbol);
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    61
    Quote Originally Posted by Malcolm McLean View Post
    If using UTF-8, just create the degree symbol manually.

    Code:
    const char degreesymbol[3] = {194, 176, 0};
    
    firstdegree = strstr(searchstring, degreesymbol);
    That very interesting. Last night I figured I could use `strchr` with 194, as it is the only multibyte character. I don't know why I didn't think of expanding to both and using `strstr`. Ty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2012, 08:49 PM
  2. Who has a degree?
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 03-17-2006, 01:53 PM
  3. Getting a degree
    By jverkoey in forum Game Programming
    Replies: 35
    Last Post: 03-24-2004, 08:38 PM
  4. how many credits does it take to get a degree?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-20-2003, 11:53 PM
  5. Are you happy with your degree?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 09-12-2002, 09:59 AM

Tags for this Thread