Replace a character of a string with an asterisk

0

The question is this: Make a program that reads a text (string) typed by the user with a maximum of 500 characters. Print this text by replacing the first letter of each word with an asterisk '*'.

Read the 500 characters I was able to do:

printf("Digite uma palavra (tamanho maximo de 500 letras):");
gets(palavra);

I'm having trouble figuring out what to do. I need to palavra[i]!='for'

If using %code% would it look something like this?

for (i=0; palavra[i]!='
printf("Digite uma palavra (tamanho maximo de 500 letras):");
gets(palavra);
' ; i++ ) { for (i=0 ; palavra[i]<'
for (i=0; palavra[i]!='%pre%' ; i++ )
{
    for (i=0 ; palavra[i]<'%pre%'; i++ )
    { 
        while (palavra[i]==palavra[i+1])
        {
           palavra[i]=' ';
           achou=1;
           printf ("%c\n\n", palavra[i]);
           i++;
        }
    }
}
'; i++ ) { while (palavra[i]==palavra[i+1]) { palavra[i]=' '; achou=1; printf ("%c\n\n", palavra[i]); i++; } } }
    
asked by anonymous 24.09.2014 / 04:05

1 answer

1

To iterate over a string in C is usually done like this:

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

int main() {
    char s[128];
    gets(s);
    for (int i = 0; i < strlen(s); ++i) {
        printf("%c", s[i]);
    }

    return 0;
}

What you wrote does pretty much the same thing, but you do not need for within the first for .

Now, usually a word and a set of characters between spaces. So, every time you find a space you can change the next letter to a '*'. Something like:

if (s[i] == ' ') {
    s[i + 1] = '*';
}

Clearly, being careful to see if the string does not end with a space.

    
24.09.2014 / 05:05