Thread: Printing charcter by character

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Printing charcter by character

    Hi.....
    I have to parse through an input XML file line-by-line, character-by-character and find if closing HTML tags are present for every opening HTML tag.

    Code:
    MAIN program:
    	while (scanf("%s", code) != EOF)
    	{
    		i=0;
    		length = strlen(code);
    		while (i < strlen(code))
    		{
    			c=code[i];
    			if (c == '<')
    			{
    	                 j=0;
    
                                     do
    	                         {
    					c=code[i];
    					element[j]=c;
    					++i;
    					j++;
    				}while(c!='>');
    
    				c=code[++i];
    
    				element[j] = '\0';
    
    
    				if (element[1] == '/')
    				{
    					l=element[2];
    					length = strlen(element);
    					for (k=0;k<=length-4;k++)
    					{
    						check[k]=element[l++];
    					}
    					check[k] = '\0';
    					/*Function passsed tho process the element()*/
    					printf("%s",element);
    
    				}
    
    				else
    				{
    					l=element[1];
    
    					length = strlen(element);
    					for (k=0;k<=length-3;k++)
    					{
    						check[k]=element[l++];
    
    					}
    					check[k]='\0';
    					/*Function passsed tho process the element()*/
    					printf("%s",element);
    				}
    
    			}
    			else
    			{
    				putchar(c);
    				c=code[++i];
    			}
    
    		}
    	}
    When printing to the output I find that any character that is printed just after '>' character without any spaces will not be printed to the output. Also no spaces from the input text is printed to the output.

    Please help
    Last edited by Ron; 06-15-2008 at 08:08 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    c=code[++i] skips past the character after the > (since i was also incremented in the search for the closing > loop as well). It is not possible to read a space using %s.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    For the spaces just use fget() reading and checking for the '\n' character.
    (Generally I prefer fget(). You have more control and you just have to use it in a while-loop checking for EOF or '\n' to read a line)

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    151
    well i tried removing the line ++i. But still same result. Also, only characters between < & > will be stored in an array and printed as &#37;s, rest will be displayed character-by-character.

    If I were to use fgets(), I will have to use FILE input/ouput. The code will get more complicated then. Our assignment mentions to use standard input. so I can use scanf, gets(), etc.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Ron View Post
    Also, only characters between < & > will be stored in an array and printed as %s, rest will be displayed character-by-character.
    How is that different than what you want?

    Quote Originally Posted by Ron View Post
    If I were to use fgets(), I will have to use FILE input/ouput.
    That's the silliest thing I've ever heard.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    151
    I have to take each line from text as string, parse through it character-by-character. When it encounters <, search for >, make it as a string, if it does not have '!', push it into a stack, else pop the stack.the rest of the contents will be displayed character by charater.

    eg. <title>I am a boy<!title>

    Dont I have to FILE stream to use fgets()?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use stdin as the argument for a FILE stream to fgets() if you want to read from standard in (which is usually the keyboard at the console).

    Example:

    Code:
    fgets(..., ..., stdin);

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    151

    Re:

    can't I use any other functions besides FILE stream like gets() that will read even space character?

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    gets() is broken. Use fgets(). It doesn't require much more work, and it's proper and safe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  2. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  3. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing a Character from Binary
    By SavesTheDay in forum C Programming
    Replies: 6
    Last Post: 02-19-2002, 02:35 PM