Converting characters in html?

This is a discussion on Converting characters in html? within the C Programming forums, part of the General Programming Boards category; This program is supposed to replace & with &amp; and < with &lt; and > with &gt; and " with ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    38

    Converting characters in html?

    This program is supposed to replace & with &amp; and < with &lt; and > with &gt; and " with &quot; or at least that is my understanding of what the question is asking. It must use the header provided. I wrote this code, but it returns the wrong results or just the first character.
    Sorry for the some what confusing way of the program, any help would be greatly appreciated.
    Code:
    char * toHTML(const char *src){
    	int n=0;
    	int size=0;
    	int i=0;
    	int t=0;
    	size=strlen(src);
    	n=size;
    	int totalchanges=0;
    	int changes=0;
    	int factorA= 5-1;
    	int factorB= 4-1;
    	int factorC= 4-1;
    	int factorD= 6-1;
    	for(i=0; i<n; i++) {
    		if(src[i] == '&') {
    			changes++;}}
    	totalchanges=(changes)*(factorA);
    	changes=0;
    	for(i=0; i<n; i++) {
    		if(src[i] == '<') {
    			changes++;}}
    	totalchanges+=(changes)*(factorB);
    	changes=0;
    	for(i=0; i<n; i++) {
    		if(src[i] == '>') {
    			changes++;}}
    	totalchanges+=(changes)*(factorC);
    	changes=0;
    	for(i=0; i<n; i++) {
    		if(src[i] == '"') {
    			changes++;}}
    	totalchanges+=(changes)*(factorD);
    	size+=totalchanges;
    	char *result = NULL;
    	result = malloc(sizeof(char) * (size + 1));	
    	for(i=0; i<n; i++){
    		if(src[i] == '&') {//&amp;
    			result[t]='&';
    			result[t+1]='a';
    			result[t+2]='m';
    			result[t+3]='p';
    			result[t+4]=';';
    			t+=4;}
    		if(src[i] == '<') {
    			result[t]='&';
    			result[t+1]='l';
    			result[t+2]='t';
    			result[t+3]=';';
    			t+=3;}
    		if(src[i] == '>') {
    			result[t]='&';
    			result[t+1]='g';
    			result[t+2]='t';
    			result[t+3]=';';
    			t+=3;}
    		if(src[i] == '"') {
    			result[t]='&';
    			result[t+1]='q';
    			result[t+2]='u';
    			result[t+3]='o';
    			result[t+4]='t';
    			result[t+5]=';';
    			t+=4;}
    		if((src[i] != '&')&& (src[i] != '<')&& (src[i] != '>')&& (src[i] == '"')){
    			result[t]=src[i];}
    		t++;
    		}
    	return result;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Where do you mark the end of the result with a \0?

    Also, consider this program logic
    Code:
    if ( src[i] == '&' ) {
    } else if ( src[i] == '<' ) {
    } else {
      result[t++]=src[i];
    }
    Also, rather than copy/paste the same code 4 times, consider something like
    count = countSpecialCharacter(src,'&');
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Champaign, Illinois, United States
    Posts
    27
    A little more information about what is going wrong would help to debug the program. I agree with adding "else", not really needed but will save a little bit of computation time.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your "factor"s are all one less than they should be, and you add one less than you should to t in your t+=... statements. And you need to zero-terminate result before you return it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    38
    I solved my error in the program now and it works great thanks to all of you. The majour problem was the else statement was not for all the ifs but just the last one. I replaced the else with another if statement and changed what was said above and all is good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting numbers to characters?
    By Aslan14 in forum C Programming
    Replies: 3
    Last Post: 12-14-2010, 02:22 PM
  2. Converting Numeric Characters to Hex
    By Pharrox in forum C++ Programming
    Replies: 13
    Last Post: 11-20-2008, 09:55 AM
  3. Converting html and work with tags
    By pb2000 in forum C Programming
    Replies: 5
    Last Post: 04-19-2005, 03:31 PM
  4. Converting C strings to list of characters
    By kazbo in forum C Programming
    Replies: 11
    Last Post: 02-14-2005, 09:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21