erasing some words

This is a discussion on erasing some words within the C Programming forums, part of the General Programming Boards category; Hi All Ive got a problem, say i have a few characters read from a file and it happens to ...

  1. #1
    cman
    Guest

    erasing some words

    Hi All

    Ive got a problem, say i have a few characters read from a file and it happens to be "<*hello*>" how would i be able to remove the characters between < & > . ? Any hints would be appreciated.

    Code:
    #include <stdio.h>
    
    int main()
    {
    int ch, inbracket = 0;
    printf( "Enter Text File: " );
    while ( (ch=getchar()) != EOF ) {
    if ( ch == '<' ) {
    if ( (ch=getchar()) == '*' )
    inbracket = 1;
    }
    im kinda stuck at this point, not sure how to remove the characters.

  2. #2
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    I have not tried it but am sure it should work.
    After you check for existence of < character
    make a null while loop like while(getchar()!='>');
    It should discard all characters upto > ( I suppose)

    If I get more time i will try to work out that problem.

  3. #3
    Dev
    Dev is offline
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    This is what I have tried.

    Try making changes to it according to your requirement like existence of '*' character.

    It works fine.

    #include <stdio.h>

    int main()
    {
    char ch;
    char str[100];
    int pos=0;
    printf("Enter input string \n");
    while((ch=getchar())!='\n')
    {
    if(ch=='<')
    while(getchar()!='>');
    else
    str[pos++]=ch;
    }
    str[pos]='\0';
    printf("\n Converted string is :-\n");
    printf("%s",str);
    getch();
    }

    If this is not what you want ( atleast logically ) then first post your logic in terms of algorithm.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int c;
      int Inside = 0;
      while ((c = getchar()) != EOF && c != '\n')
      {
        if (c == '<') Inside = 1;
        if (!Inside)
        {
          putchar (c);
        }
        if (c == '>') Inside = 0;
      }
      return 0;
    }
    
    /*
     Output
    
    This is <blah blah> a test
    This is  a test
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    cman
    Guest
    what if you need to characters to determine a starting point and an ending point. E.g. start: "<*" end: "*>"
    can you use getchar twice?

    im stuck with two values but okay with one character, unable to use arrays since havent learnt it yet.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just "remember" the previous character that you read in another variable, the compare that with the current.

    >>if (prev=='<' && current=='*')

    or you could read a line at a time, and use strstr().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM

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