Sponsored Content
Full Discussion: char problem ?
Top Forums Programming char problem ? Post 302393786 by cyler on Tuesday 9th of February 2010 02:58:13 PM
Old 02-09-2010
char problem ?

Here is a C function that replaces some non-ASCII chars to html decimal entities. It seems that the char "ç" does not get replaced correctly but the rest do. Any idea why this is happening ?

(Please note that I had to place a space before each ; or they would not post correctly in this forum because the browser would convert them automatically.)

The function outputs:
Bar&#38 ;#38 ;#38 ;#38 ;#231 ;a
but it should output:
Bar&#231 ;a

Code:
char *conversionpointer = NULL;

char *chartodecimal(const char *STRING) // replaces all occurrences of char to html decimals
{
    conversionpointer = (char *)realloc(conversionpointer, strlen(STRING) + 1);

    strcpy(conversionpointer, STRING);

    char original[15][3] = {        "ç",   "\"",    "&",     "ñ",     "ä",     "é",     "ë",     "ü",     "ã",     "º",     "ª",     "á",     "ó",     "ø",     "ß"};
    char replacement[15][7] = {"&#231 ;","&#34 ;","&#38 ;","&#241 ;","&#228 ;","&#233 ;","&#235 ;","&#252 ;","&#227 ;","&#186 ;","&#170 ;","&#225 ;","&#243 ;","&#248 ;","&#223 ;"};

    char *conversionpointeroriginalp = NULL;
    char *buffer = NULL;
    int count = 0;

    while(count < 15)
    {
        conversionpointeroriginalp = strstr(conversionpointer, original[count]);

        while(conversionpointeroriginalp != NULL)
        {
            buffer = (char *)realloc(buffer, strlen(conversionpointer) + 1 + (strlen(replacement[count]) - strlen(original[count])));
            strncpy(buffer, conversionpointer, (size_t)(conversionpointeroriginalp - conversionpointer));

            sprintf(buffer + (conversionpointeroriginalp - conversionpointer), "%s%s", replacement[count], conversionpointeroriginalp + strlen(original[count]));

            conversionpointer = (char *)realloc(conversionpointer, strlen(buffer) + 1);

            strcpy(conversionpointer, buffer);

            conversionpointeroriginalp = strstr(conversionpointeroriginalp, original[count]);
        }

        count++;
    }

    if (buffer != NULL)
    {
        free(buffer);
    }

    return conversionpointer;
}

int main(void)
{
    printf("%s\n", chartodecimal("Barça"));

    return 0;
}

 

10 More Discussions You Might Find Interesting

1. Programming

char array problem

hello i have a program in C (Unix - SOlaris5.7), and i have the next question: i have a lot of char variable, and i want store their values in a char array. The problem is what i don´t know how to put the char variable's value into the array, and i don`t know how to define the array please... (4 Replies)
Discussion started by: DebianJ
4 Replies

2. Programming

C programming + problem with char arrays

Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder. Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making... (7 Replies)
Discussion started by: JamesGoh
7 Replies

3. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

4. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

5. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

6. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

7. Programming

"char" memory layout problem!

In the following code, why the final result of "usC=cA+(char)ucB;" is 0xFF00? In my opioion the memory layout of cA is "10000000" and (char)cB is "10000000",usC type is unsigned short ,so the result should be "100000000" ,the 0x100. Please help tell me what is wrong? Thanks!!;) ... (2 Replies)
Discussion started by: micky.zhou
2 Replies

8. UNIX for Dummies Questions & Answers

Problem in C shell (csh) prompt setting containing the '$' char

Hi, I am trying to customize the command prompt of the C shell as follows: set prompt=" " The above one works fine but when I try to add a '$' (dollar) symbol into the string as set prompt=" " I am getting the error as: Illegal variable name However, this one set prompt = "-- %T %n %~ --... (2 Replies)
Discussion started by: royalibrahim
2 Replies

9. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

10. UNIX for Advanced & Expert Users

Perl regex problem on strings with several occurences of one char

Hi all, i have the following line in a record file : retenu=non demande=non script=#vtbackup /path=/save/backup/demande position=140+70 and i want to use Perl regex to have the following output key : "retenu" value : "non" key : "demande" value "non" key : "script" value :... (2 Replies)
Discussion started by: Fundix
2 Replies
STRING(3)						     Library Functions Manual							 STRING(3)

NAME
strcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, strcpy, strncpy, strlen, index, rindex - string operations SYNOPSIS
#include <strings.h> char *strcat(s, append) char *s, *append; char *strncat(s, append, count) char *s, *append; int count; strcmp(s1, s2) char *s1, *s2; strncmp(s1, s2, count) char *s1, *s2; int count; strcasecmp(s1, s2) char *s1, *s2; strncasecmp(s1, s2, count) char *s1, *s2; int count; char *strcpy(to, from) char *to, *from; char *strncpy(to, from, count) char *to, *from; int count; strlen(s) char *s; char *index(s, c) char *s, c; char *rindex(s, c) char *s, c; DESCRIPTION
These functions operate on null-terminated strings. They do not check for overflow of any receiving string. Strcat appends a copy of string append to the end of string s. Strncat copies at most count characters. Both return a pointer to the null- terminated result. Strcmp compares its arguments and returns an integer greater than, equal to, or less than 0, according as s1 is lexicographically greater than, equal to, or less than s2. Strncmp makes the same comparison but looks at at most count characters. Strcasecmp and strncasecmp are identical in function, but are case insensitive. The returned lexicographic difference reflects a conversion to lower-case. Strcpy copies string from to to, stopping after the null character has been moved. Strncpy copies exactly count characters, appending nulls if from is less than count characters in length; the target may not be null-terminated if the length of from is count or more. Both return to. Strlen returns the number of non-null characters in s. Index (rindex) returns a pointer to the first (last) occurrence of character c in string s or zero if c does not occur in the string. Set- ting c to NULL works. 4th Berkeley Distribution October 22, 1987 STRING(3)
All times are GMT -4. The time now is 05:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy