First letter of a word in C

0

I need to make a program that takes out the first letter of a word through a function in C, without using characteristics of Arrays.

I have this code:

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

char dimeLetra(char[]);

int main(){
  char palabra[20];
  char letra;
  printf("Introduce tu palabra: ");
  scanf("%s",&palabra);
  letra = dimeLetra(&palabra);
  printf("%c",letra);
}

char dimeLetra(char p[]){
  printf("%s",p);
  char l;
  strncpy(l,p,0);
  return l;
}

I can not get the dimeLetra function to return the first letter of the word I send to it.

Any ideas?

    
asked by lk2_89 21.10.2018 в 01:15
source

2 answers

1

Here is an example of how you can do it. Hope this can help you. Greetings!

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

char palabra[20];
int cantLetras = 1; // Elijo la cantidad de letras que deseo saber.(Izq a derecha)

  dimeLetra(char text[]){

   char buffer[512]; // Buffer para copiar la mitad

   strncpy(buffer,text,cantLetras);

   printf("\rPrimer letra:\t%s",buffer); // Visualizacion de la cadena
}
int main()
{
    printf("Introduce tu palabra: ");
    scanf("%s",&palabra);

    dimeLetra(palabra);

   return 0;
}
    
answered by 21.10.2018 в 03:35
1

The first letter of a character formation in C is the content of the training pointer:

char dimeLetra(char p[]){
  return *p;
}
    
answered by 24.10.2018 в 08:32