Thread: Censoring Text In C

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    22

    Post Censoring Text In C

    I am trying to censor a word in a sentence by replacing all the letters with aesterisks (*). But I only know how to replace the word with one aesterisk and when I space out the sentence it doesn't recognise all the words in the sentence to censor. Any help would be appreciated!

    insert
    Code:
    #include<stdio.h>  
    #include<string.h>  
      int main() {
            char str[256] = "prepinsta", substr[128] = "insta", replace[128] = "* ", output[256];
            int i = 0, j = 0, flag = 0, start = 0;
            
            
            printf("Enter your string:");
            scanf("%s",str);
            
            printf("Enter your sub-string:");
            scanf("%s",substr);
            
            
            
                                          str[strlen(str) - 1] = '\0';
            substr[strlen(substr) - 1] = '\0';
            replace[strlen(replace) - 1] =  '\0';
    
    
            // check whether the substring to be replaced is present 
            while (str[i] != '\0')
            {
                    if (str[i] == substr[j]) 
                    {
                            if (!flag)
                                    start = i;
                            j++;
                            if (substr[j] == '\0')
                                    break;
                            flag = 1;
                    } 
                    else 
                    {
                            flag = start = j = 0;
                    }
                    i++;
            }
            if (substr[j] == '\0' && flag)
            {
                    for (i = 0; i < start; i++)
                            output[i] = str[i];
    
    
                    // replace substring with another string 
                    for (j = 0; j < strlen(replace); j++) 
                    {
                            output[i] = replace[j];
                            i++;
                    }
                    // copy remaining portion of the input string "str" 
                    for (j = start + strlen(substr); j < strlen(str); j++)
                    {
                            output[i] = str[j];
                            i++;
                    }
                    // print the final string 
                    output[i] = '\0';
                    printf("Output: %s\n", output);
            } else {
                    printf("%s is not a substring of %s\n", substr, str);
            }
            return 0;
      }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
     
    int main() {
        printf("Enter a sentence: ");
        char sentence[1024];
        fgets(sentence, sizeof sentence, stdin);
        int len = strlen(sentence);
        if (len && sentence[len - 1] == '\n') sentence[--len] = '\0';
     
        printf("Enter a word to censor: ");
        char word[128];
        fgets(word, sizeof word, stdin);
        len = strlen(word);
        if (len && word[len - 1] == '\n') word[--len] = '\0';
     
        for (char *p = sentence; (p = strstr(p, word)) != NULL; )
            while (isalpha(*p))
                *p++ = '*';
     
        printf("%s\n", sentence);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2022
    Posts
    22

    Hey thanks for your help however I am getting a problem when compiling.

    [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Well, if you feel the need to use a very old version of the language, then just remove the for loop initial declarations. Alternatively, you could simply compile it as c99 or c11.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    May 2022
    Posts
    22

    Hey thanks is it possible to put this into a function like format?

    char * censor( char *sentence, char *word)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you could make a function like that.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-16-2019, 07:47 PM
  2. User enter text and save to text file (end enter text using -1)
    By DecoratorFawn82 in forum C Programming
    Replies: 7
    Last Post: 12-28-2017, 04:23 PM
  3. Type text = Press button = Display text in Google?
    By Raze88 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2008, 08:39 AM
  4. Censoring words
    By Sentral in forum C++ Programming
    Replies: 19
    Last Post: 08-04-2006, 03:05 PM
  5. create a text file with data using text editor
    By fried egg in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 09:11 PM

Tags for this Thread