Count letter and pick up initials of name

-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, say how many letters A has that person's name and say the first letter of their names, for example: Joao vinicios, the program will show me JV

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;   
}
    
asked by anonymous 22.11.2017 / 21:37

2 answers

5

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

You do not need two loops, you just have to go through all the text to parse character by character. You can do both analyzes within it.

From what I understand, you just have to print the initials, so it's simpler than you were trying to do. You just need to correct the condition that you consider an initial, l because it is the one that determines if another word began.

I considered the uppercase or lowercase letter.

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

Obviously it only matters if it is a letter. If it is it 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 Coding Ground . Also put it in GitHub for future reference .

    
22.11.2017 / 22:00
1

Some things:

Instead of nome[i]!=0 you have to nome[i]!='char' . In the first case you are comparing a so to an integer.

Then you do:

soma=so

What does not make sense, nor from the point of view of language ( ; has not been defined, besides not having soma at the end of the line), as it is not necessary for the problem, since nome[i]=nome[i+1] only counts the number of A.

After you do i+1 , it is 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]!='
for(i=0; nome[i+1]!='
soma=so
'; i++){ if(i==0) printf("%c\t", nome[i]); //Imprime a primeira inicial if(nome[i]=' ' && nome[i+1]!='
for(i=0; nome[i+1]!='
for(i=0; nome[i+1]!='%pre%'; i++){
    if(i==0) printf("%c\t", nome[i]); //Imprime a primeira inicial
    if(nome[i]=' ' && nome[i+1]!='%pre%')
        printf("%c\t", nome[i+1]); //Imprime as outras iniciais
}
'; i++){ if(nome[i]=' ' && nome[i+1]!='%pre%') printf("%c\t", nome[i+1]); }
') printf("%c\t", nome[i+1]); //Imprime as outras iniciais }
'; i++){ if(nome[i]=' ' && nome[i+1]!='%pre%') printf("%c\t", nome[i+1]); }

Note that the loop should check if the character in i+1 is the end of the string, since you access the %code% element within the loop.

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

%pre%     
22.11.2017 / 22:00