|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#21 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Ma che mi serve trovare l'inizio e la fine di una parola ( parliamo di indici del vettore, come inizio e fine, giusto?).
Quello che ho tentato di fare è usare la funzione strncat per accodare al vettore word n caratteri del vettore str_line... Il fatto è che con questa funzione non si può dire da che indice del vettore partire...
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#22 | |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quote:
Devi usare l'aritmetica dei puntatori o l'operatore &. Se io voglio copiare 10 caratteri dal carattere s[5]: strncat(dest, &s[5], 10); oppure strncat(dest, s + 5, 10); Comunque fai quello che ti ho detto e vedrai che potrai utilizzare la stessa funzione in entrambe le funzioni che devi fare. |
|
|
|
|
|
|
#23 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Scusami un attimo, qua mi sto perdendo...
Allora sto cercando di fare un dannato passaggio per riferimento dal main ad una funzione secondaria... Ho scritto così ( non è uguale il codice perchè ho pasticciato e non c'ho voglia di aggiustarlo Codice:
int chiama_funzione( int *n);
main ()
{
int n=0;
chiama_funzione ( &n );
}
int chiama_funzione( int *n)
{
*n++;
}
Come cacchio devo fare??? Sta cosa mi fa imbestialire perchè è una baggianata, mi pare di aver fatto così con altri programmi e ha sempre funzionato, qua no invece... Non cambia nulla se metto n++ invece di *n++ .... Quello che cercavo di fare era avere il valore di quante parole hanno la lunghezza massima. Siccome col return passo già la lunghezza massima, volevo fare un passaggio per riferimento alla funzione così da poter modificare il valore nel main.
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#24 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
(*n)++
|
|
|
|
|
|
#25 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#26 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Alla fine sono riuscito a venirne a capo.
Il suggerimento di cionci di saltare ad un indice x di un array tramite gli indirizzi è stato provvidenziale. Codice:
/*
Write a C program that reads a line of text from keyboard and then writes the longest word in the line.
If more than one word has longest length, the program will output all of them.
The program should work with lines up to 132 characters long and ignore characters in excess.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 133
int get_line ( char str_line[MAX] );
void erase_array ( char array[MAX]);
int longest_word ( char str_line[MAX], int str_len, int *n);
void get_word ( char str_line[MAX], char word[MAX], int max_lenght, int str_len, int *actual_index, int *flag);
int main (void)
{
int string_lenght=0; //This gets the lenght of the array and checks if there's any error
char str_line[MAX];
char word[MAX];
int max_lenght=0;
int n=0; //How many words has the max len?
int i=0;
int actual_index=0;//Stores the position reached by the get_word function in analyzing words
int flag=0; //Checks if word has been filled
erase_array( str_line );
printf("Insert a line of text\n");
printf("The line cannot exceed 132 characters\n");
if ((string_lenght = get_line ( str_line )) == -1)
{
printf("Error in reading the line or line is empty");
return -1;
}
else
{
max_lenght = longest_word ( str_line, string_lenght, &n );
if ( n > 1)
{
printf("There are %d words with maximum lenght of %d\n", n, max_lenght);
do
{
get_word ( str_line, word, max_lenght, string_lenght, &actual_index, &flag);
if ( flag == 1)
{
puts(word);
printf("\n");
i++;
}
} while ( i < n );
}
else {
printf("The longest word has %d characters\n", max_lenght);
while ( flag == 0)
{
get_word( str_line, word, max_lenght, string_lenght, &actual_index, &flag);
if ( flag == 1)
{
puts(word);
}
}
}
}
return 0;
}
void erase_array (char array[MAX])
{
int i;
for ( i=0; i < MAX; i++ )
array[i]=0;
}
int get_line ( char str_line[MAX] )
{
int max_str_lenght = MAX - 1; //We reserve the last slot for \0
int i=0;
if ( fgets( str_line, max_str_lenght, stdin)== NULL)
return -1;
else if( (i = strlen(str_line))== 0 )
return -1;
printf("Array has lenght: %d\n", i-1); // i-1 because it counts also \0 and we don't want this
return i-1;
}
int longest_word ( char str_line[MAX], int str_len, int *n)
{
int i=0;
int temp_len=0; //Stores temporary lenght
int max_len=0; //Stores maximum lenght
for ( i=0; i < str_len; i++)
{ temp_len = 0;
while ( str_line[i] != ' ' && str_line[i] != '\0' && str_line[i] != '\n')
{
if ( isalpha(str_line[i]) && str_line[i] != '\n' )
{
temp_len++;
i++;
}
}
if ( temp_len >= max_len )
max_len = temp_len;
}
for ( i=0; i < str_len; i++)
{
temp_len=0;
while( isalpha(str_line[i]))
{
temp_len++;
i++;
if ( temp_len == max_len)
(*n)++; //We increase by one the counter every time we find a word with max lenght
}
}
return max_len;
}
void get_word ( char str_line[MAX], char word[MAX], int max_lenght, int str_len, int *actual_index, int *flag)
{
int i, j;
int temp_len=0;
erase_array(word);
for ( i = *actual_index, j = 0; isalpha(str_line[i]); j++, i++)
;
*actual_index = i + 1;
if ( j == max_lenght)
{
temp_len = j;
i -= max_lenght;
strncpy(word, str_line + i, max_lenght);
*flag = 1;
}
else
*flag = 0;
str_line += *actual_index;
}
Il programma non è sicuramente ottimizzato, ho infatti notato che alcuni if possono essere tolti e alcuni controlli sono inutili, ma non è ciò che mi interessa. L'obiettivo di completare il programma e renderlo funzionante è riuscito, quindi fine. Grazie a tutti per l'aiuto e al prossimo programma
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 14:35.




















