Count letters and pick initials of the name

Asked

Viewed 3,763 times

-2

The program does not print the first letter of each name.

nome[i]=nome[i+1];

I need to make a program that enters the name of the person, tells how many letters A has the name of that person and says the first letter of their names, for example: Joao Vinicios, the program will show me J.V.

This is what I’ve done so far:

#include <stdio.h>
#include <conio.h>

int main()
{
    char nome[99];
    int soma,i;

    printf("Digite o nome completo:\n");
    gets(nome);

    soma=0;
    for(i=0;nome[i] != 0; i++) //For para mostrar quantos a tem o nome da pessoa
    {
        if(nome[i]=='a')
        {
            soma=soma+1;
        }
    }

    for(i=0; nome[i]!=0; i++) // For para pegar a primeira letra de cada nome da pessoa 
    {
        if(nome[i] == ' ')
        {
            soma=so
            nome[i]=nome[i+1];
            printf("%s\n",nome[i]);
        }
    }

    printf("Seu nome tem %d letras 'A'" ,soma); // imprimi na tela quantos A tem o nome da pessoa

    getch();
    return 0;   
}
  • You need to report what your specific problem is.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

5

There are several problems there. First I eliminated the conio not recommended to use. And I simplified the code.

It doesn’t need two loops, you just have to scroll through the text to parse character by character. You can do both analyses inside it.

From what I understand, just print the initials, so it’s simpler than what I was trying to do. Just need to correct the condition you consider an initial, because it is it that determines whether you started another word.

I considered the letter uppercase or lowercase.

There are other criteria that may give problem in what is initial. I showed one of them that is the word that is not considered part of the name (da), but it could have numbers and symbols that would cause trouble.

Obviously, it only matters if it’s a letter. If it is you can accept it as initial if it is the first character being analyzed or if the previous one is a space.

#include<stdio.h>

int main() {
    char nome[99];
    printf("Digite o nome completo:\n");
    fgets(nome, 98, stdin);
    int qtdA = 0;
    for (int i = 0; nome[i] != 0; i++) {
        if (nome[i] == 'a' || nome[i] == 'A') qtdA++;
        if (nome[i] != ' ' && (i == 0 || nome[i - 1] == ' ')) printf("%c.", nome[i]);
    }
    printf("\nSeu nome tem %d letras 'A'" , qtdA);
}

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

1

Some things:

Instead of nome[i]!=0 you have to do nome[i]!='\0'. In the first case you’re comparing a char integer.

Then you do:

soma=so

Which makes no sense, even from the point of view of language (so has not been defined, in addition to not having the ; at the end of the line), as is not necessary for the problem, since soma only counts the number of A.

Then you do nome[i]=nome[i+1], not necessary, consider that after a space is the first letter of the name, just check if the next character exists and print it.

for(i=0; nome[i+1]!='\0'; i++){
    if(nome[i]=' ' && nome[i+1]!='\0')
        printf("%c\t", nome[i+1]);
}

Note that the loop should check whether the character in i+1 which is the end of the string, since you access the element i+1 inside the loop.

Also note that with the loop structured this way the first initial is ignored, since you only check the initial after a space, for this just insert an if:

for(i=0; nome[i+1]!='\0'; i++){
    if(i==0) printf("%c\t", nome[i]); //Imprime a primeira inicial
    if(nome[i]=' ' && nome[i+1]!='\0')
        printf("%c\t", nome[i+1]); //Imprime as outras iniciais
}
  • this sum=so was a typo I already took it, I will try here vlw

Browser other questions tagged

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