How to test the condition on a vector?

Asked

Viewed 658 times

7

I have to make a program that reads a text, where each first letter of each word in the text is replaced by a character (*). I cannot 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);

}
  • What is each first letter? First letter of a word?

  • fflush(stdin) is only correct for Microsoft libraries (for other libraries is Undefined Behavior); gets() is impossible to use safely. I suggest you review your program to not use these constructions.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

4 answers

4

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

#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);

}

Missing to include stdio.h to use the printf.

  • Not necessarily the first position, but each space of the text should be added a character. EX: *ma *alavra * *iferente *e *m *Text.

  • How do I make the first letter of the first word also be replaced?

  • @Daviaragao if you type space and a name any your algorithm will no longer be valid.

  • @Gabriel Rodrigues you were right thank you for alerting me. It is corrected.

4

There are some errors, including 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I gave an organized in the code and changed the reading function because the one used is obsolete and insecure. I 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 the newer compilers (not so new) generate error when using this function and do not compile.

Has a Ask here what you’re talking about.

There’s another one in the English OS.

  • Your bigown program is good, meets the need, but I am learning and my teacher does not accept that we make use of methods not yet learned.

  • Which method has not been learned?

  • These functions fgets, sizeof. Even if you have an indication of bibliography, site for me to search thank!

  • 3

    So change teacher :) The sizeof can change by MAX. The fgets is not an option. It’s not my taste. No one should use gets in any program. They are teaching you to do wrong things. I would question the course you are doing. What’s the use of learning to do wrong?

  • Show initiative, do it the right way, show your teacher and classmates that you researched and found a better way to do it. I put more information in the answer that can support you to use the function. But please learn to do it right. I hate to see people learn wrong. Anyway, even though you haven’t found my solution better yet, you can vote for her if you think she’s at least good. You can vote for anything on the site. See the [tour].

  • Let me do my best.

  • And the other solution does not do what you want. Now that I see that you have taken acceptance, you must have seen that there is error in it.

Show 2 more comments

2

Funny, your code compiled normally, the only thing I had to add to overwrite all first letters with * was an if checking if the first position is different from empty, so when the user type the first letter with space will not give 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);

}

Testing:

Mussum ipsum cacilds, vidis litro abertis.

Upshot:

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

  • He edited the question by putting the code "right". I went back to the original.

1

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

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

Each character should be only 1 symbol.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.