string manipulation-II:code problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apsonline
    New Member
    • Aug 2006
    • 10

    string manipulation-II:code problem

    hi
    m trying to write a code that would find an abusive word from a given a string and then compare this word with the list of abusive words, maintained in an array char abusive[20], and replace them with ####.

    this is how m coding it.
    firstly get the first word of the string and then compare this word with all the abusive words in abusive[], and if found then replace it by ####

    void main()
    {
    int i=0;
    clrscr();
    char ch[20],*s,ch1[20];
    cout<<"Enter ur sentence:\n";
    gets(ch);
    s=ch;
    //while there is a character to be read ,move on

    while(*s!='\0')
    {
    if(*s==' ') // if found a 'space' ,ie if found a ' word'
    {
    goto l1;

    }
    else //if not, ,just copy the content of the given string into another string
    {
    ch1[i]=*s;
    i++;
    s++;
    }

    }
    l1:
    puts(ch1); //print the word just found

    //Problem 1:
    here ,how to make the ch1[] empty again?(so that next time it again contains the word found after the space)

    // now compare this word with the list
    for(int i=0;i<20;i++)
    {
    if(abusive[i]==ch[??])// Prblem 2:ch[20] or just ch[]
    {

    //Problem 3: how to replace this abusive word just found by ####?
    }

    getch();

    }
  • apsonline
    New Member
    • Aug 2006
    • 10

    #2
    anyone??plz help me out

    Comment

    • apsonline
      New Member
      • Aug 2006
      • 10

      #3
      please reply urgently

      Comment

      • anitajose
        New Member
        • Aug 2006
        • 11

        #4
        Originally posted by apsonline
        please reply urgently
        Try this

        #include <iostream.h>
        #include<string .h>
        #include<stdio. h>

        void main()
        {
        int i=0;
        char ch[20],*s,ch1[20];
        printf("Enter ur sentence:\n");
        gets(ch);
        s=ch;
        //while there is a character to be read ,move on

        while(*s!='\0')
        {
        if(*s==' ') // if found a 'space' ,ie if found a ' word'
        {
        goto l1;

        }
        else //if not, ,just copy the content of the given string into another string
        {
        ch1[i]=*s;
        i++;
        s++;
        }
        ch1[i]='\0';

        }
        l1:
        puts(ch1); //print the word just found
        strcpy(ch1,"");
        puts(ch1);
        }

        Comment

        • pukur123
          New Member
          • Sep 2006
          • 61

          #5
          Try this one. It will definitely serve your purpose........


          #include<stdio. h>
          #include<string .h>

          int main()
          {
          char str[100], *abusive[20]={"abusive1", "abusive2","fil l the rest of the words"};
          char word[25];
          int i, j=0, flag, k, len, p, m;

          printf("Enter a string ");
          fflush(stdin);
          gets(str);

          for(i=0; str[i] != '\0'; i++)
          {
          if(str[i] == ' ' || str[i+1] == '\0')
          {
          if(str[i+1] == '\0')
          word[j++]=str[i];

          word[j]='\0';

          j=0;

          flag=0;
          for(k=0; k<=2; k++)
          {
          if(strcasecmp(a busive[k], word) == 0)
          {
          flag=1;
          break;
          }
          }

          if(flag == 1)
          {
          len=strlen(word );

          if(str[i] == ' ')
          {
          p=i-len;

          for(k=i+1; str[k] != '\0'; k++)
          str[k-len-1]=str[k];
          str[k-len-1]='\0';

          for(m=k-len-1; m>=p; m--)
          str[m+5]=str[m];
          str[p+4]=' ';

          for(m=p; m<=p+3; m++)
          str[m]='#';

          i=p+4;
          }
          else
          {
          p=i-len+1;

          for(m=p; m<=p+3; m++)
          str[m]='#';
          str[m]='\0';

          i=m-1;
          }
          }
          }
          else
          word[j++]=str[i];
          }

          printf("%s\n",s tr);

          return 0;
          }

          Comment

          Working...