How to test the condition on a vector?

7

I have to do a program that reads a text, where each first letter of each word of the text is replaced by a character (*). I can not compile correctly, I believe the problem is in my condition ( if ).

#include <stdlib.h>
#include <string.h>
#define MAX 500

int main()
{
char text [MAX+1];
int i;

printf("Informe o texto (tamanho maximo %d caracteres:", MAX);
fflush(stdin);
gets(text);

for (i=0; i<=MAX; i++) {
if(' text[i]'==text[i]);
text[i]=' *';

}
printf(" O texto final e %s\n", text);

}
    
asked by anonymous 21.09.2015 / 00:37

4 answers

4

It has some errors, even it does not even compile:

#include <stdio.h>
#define MAX 500

int main() {
    char text [MAX+1];

    printf("Informe o texto (tamanho maximo %d caracteres:", MAX);
    fgets(text, sizeof(text), stdin);
    text[0] = '*';
    for (int i = 0; i <= MAX; i++) {
        if (text[i] == ' ') {
            text[i + 1] = '*';
        }
    }
    printf("\nO texto final e %s\n", text);
}

See running on ideone .

I gave an organized in the code and changed the reading function because the used is obsolete and insecure. It can improve but I think it solves the purpose.

The use of gets should not be used in any program by any programmer. So much so that newer (or new) compilers generate error when using this function and do not let them compile.

You have a question here that talks about .

There is another one in the English OS .

    
21.09.2015 / 00:50
4

If you need to put * in the first position of each word a string read from the entry try:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 500

int main()
{
    char text [MAX + 1];
    int i;
    printf("Informe o texto (tamanho maximo %d caracteres:", MAX);
    fflush(stdin);
    gets(text);

    for(i = 0; i < MAX; i++)
    {
        if((text[i - 1] == ' ') || ((i == 0) && (text[i] != ' ')))
            text[i] = ' *';
    }
    printf("O texto final e %s\n", text);

}

Failed to include stdio.h to use printf .

    
21.09.2015 / 00:42
2
Funny, your code compiled normally, the only thing I had to add to overwrite all the first letters with * was an if checking if the first position is different from empty, so when the user types the first letter with space will not give a problem.

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#define MAX 500

int main(){
    char text [MAX + 1];
    int i;
    printf("Informe o texto (tamanho maximo %d caracteres:", MAX);
    fflush(stdin);
    gets(text);

    if(text[0] != ' '){
       text[0] = ' *';
    }

    for(i = 0; i < MAX; i++){
        if(text[i] == ' '){
        text[i + 1] = ' *';
    }
}

    printf(" O texto final e %s\n", text);

}

Test:

Mussum ipsum cacilds, vidis litro abertis.

Result:

*ussum *psum *acilds, *idis *itro *bertis.

    
21.09.2015 / 02:15
1

Your biggest problem is to use "characters" with more than one symbol.

' text[i]' has 8 symbols; ' *' has 2 symbols.

Each character should be just 1 symbol.

if (' ' == text[i]) /* ... */;
text[i] = '*';

As other users have noticed, your code has other serious issues.

    
25.09.2015 / 15:38