I want to censor a word to asterisks based on the original length(Happy to *****, dog to *** for example) and my current code doesn't work. Any ideas?

Gtracks

Attempt at censoring "hello" to "*****" The error message it gives is signal: segmentation fault (core dumped).

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

int main(void) {
    char word = "hello";
    int i;
    int len = strlen(word);
    char modified[len];
    for(i=0; i<len; i++){
      modified[i] = "*";
    }
    printf("%s",modified);  
}
meagar

There are a few things wrong here.

First, char is not a string, it's a single character, which is essentially a short, single byte integer. The line char word = "hello"; takes the address of the string "hello", converts it to an integer, and assigns that integer to word. You don't actually want the numeric representation of the strings address, you want a pointer to the actual string, so your word should be a const char*, not a char.

Secondly, you don't reserve enough space for a null byte at the end of your string. Strings in C are simply sequences of characters, terminated by a null byte, and you're expected to add a null byte to your string so functions that use strings know when the string stops.

Thirdly, you're assigning the string "*" to each character in your modified array. A string is not a character, character literals are delimited with single quotes: '*'.

All in all, you can correct the issues and wind up with something like this:

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

int main(void) {
    const char *word = "hello";
    int i;
    int len = strlen(word);
    char modified[len + 1];
    for(i=0; i<len; i++){
      modified[i] = '*';
    }
    modified[len] = 0;
    printf("%s",modified);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Async/Await doesn't work in my example

Trying to reverse multiple integers in an array - want to know why my code doesn't work

I want to pass variables into my VBA with python yield using xlwings but doesn't seem to work

I have a SMSgateway JavaScript API which I want to integrate with my website, somehow it doesn't seems to work

I have 2 dropdowns, i want to search for data in the database that matches both attributes. My code below doesn't work

I want to simply send get request through android but it doesn't work my code is as follow,

Hide and Show through Mouseover doesn't work. Any idea why my code doesn't work?

IBM example code, non re-entrant functions doesn't work in my system

i try to create a navbar but my javascript code doesn't work

my code doesn't work anymore but i haven't changed it

My applescript doesn't work any more when I upgrade my OS X to 10.9

I want to fix my footer to the bottom of my content (not screen) so that it is all in one div ? Any ideas?

Any code outside 'it' doesn't work with Protractor

why my application doesn't work when I want to create buttons dynamically in android?

pushd with rd doesn't work as I want

Flattening a list recursively - why doesn't my code return what I want?

CoordinatorLayout doesn't work like I want it to

Gitignore file doesn't work as I want

I want use the values in my dict., too calculate - but it doesn't work

I want to find the # of subarrays in the array, which has the given sum. But the code doesn't work as I want

Why repaint() doesn't work when i want repaint my painting?

I want to sort my struct by alphabetical order, but if i do the sort, my program doesn't give any output

My css code in Word Press is working for every other page in the website when i only want it to work for 1 page

My code doesn’t work when I put in in a function in python

Powershell doesn't work like I want it to

I want to select the text inside input box by clicking on a button in react js but my code doesn't work

When I include scanf() in my C code it doesn't work

Did timezone() get any changes recently? Some of my code that used to work fine doesn't work anymore

My code doesn't work and I have no idea why