Announcement

Collapse
No announcement yet.

Lunghezza di una stringa

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lunghezza di una stringa

    Il programma, data in input una stringa, ne restituisce la sua lunghezza.

    PHP Code:
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    *

    * Il programma stabilisce la lunghezza di una stringa

    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    #include <st***.h>

    void lunghezza_stringa(char *stringaint *lunghezza);

    int main (void)

    {
    char *stringa;
    int lunghezza 0;

    printf("Inserisci una stringa:");
    scanf("%s"stringa);

    lunghezza_stringa(stringa, &lunghezza); // Calcolo lunghezza della stringa

    printf("La lunghezza della stringa inserita è uguale a %d.\n"lunghezza);

    return 
    0;
    }

    void lunghezza_stringa(char *stringaint *lunghezza)

    {
    int i 0// Contatore

    while (*(stringa+i) != '\0') { // Calcolo lunghezza
    i++;
    }

    *
    lunghezza i;


  • #2
    eccone la mia versione con il getchar, ovviamente per mettere i caratteri uno ad uno ho dovuto usare un array di char e non un puntatore variabile pochè gcc non permette la scrittura su puntatori a stringhe visto che sono in un'area di sola lettura
    PHP Code:
    /*
     *
     * O*** I COMMENTI PER I PROGRAMMI PICCOLI
     *
     * Dare in input una stringa e stamparne la lunghezza
     */

    #include <st***.h>
    #define SIZE 20

    int len_str(char *a); //prototype
    //////////////////////////////////////////////////////////////////// main
    int main (void){
        
    char stringa[SIZE];
        
    char temp_c;
        
    int i;
        
    //////////////////// input dati
        
    temp_c=getchar();
        for (
    i=0;temp_c!=10  && temp_c!=EOF && i<(SIZE-1);i++){
            *(
    stringa i)=temp_c;
            
    temp_c=getchar();
        }
        *(
    stringa i)=0;
        
    //////////////////// fine input dati
        
    if (len_str(stringa)==(-1)){
            
    printf("Stringa non valida\n");
        }else{
            
    printf("la stringa Ã¨ lunga %d caratteri\n",len_str(stringa));
        }
        return 
    0;
    }
    /////////////////////////////////////////////////////////////////// calcolo lunghezza stringa
    int len_str(char *a){
        
    int i;
        
        if (
    a==NULL){
            return -
    1;
        }else{
            for (
    i=0;*(a+i)!=0;i++){
            }
        return 
    i;
        }

    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio

    Comment

    Working...
    X

    Google Profile


    My name is Giovanni Di Grezia, but people call me xgiovio.

    Here is my homepage:.

    I'm a VFX Artist and Software Developer.

    Giovanni Di Grezia