|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
Stringa
Ciao a tutti....
'sto giro devo fare un programma che data una riga di testo in input la memorizza in una stringa con al+ 80 caratteri...e PER OGNI PAROLA DELLA STRINGA ne visualizzi il corrispettivo in piglatin cioe traduca ogni parola in questo modo: montagna -------> ontagnama io l'ho fatto così.....ma mi da uno strano problema di compilazione...che non capisco (all'interno della procedura).... Codice:
#include <stdlib.h> #include <stdio.h> #define DIM 80 void piglatin (char arr[], int dim) {char *p=arr; char *c; for (c=arr[1]; c<=arr[dim-1]; c++) printf ("%c", *c); printf ("%ca ",p); } int main() {int g=0; char riga [DIM]; char a [DIM]; char *p; printf ("Scrivi una riga di testo\n"); scanf ("%s",riga); for (p=riga; p<&riga[DIM]; p++,g++) {if (*p!=' ') a[g]=*p; else{ p++; piglatin (a,g); g=0;} system("PAUSE"); return 0; }
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Jun 2002
Città: Firenze
Messaggi: 630
|
Secondo me è perché confronti C (che è un puntatore) con ARR[1] e ARR[DIM-1] (che sono char).
Non so se è corretto, però io avrei confrontato C con &ARR[1] e con &ARR[DIM-1].
__________________
---> Lombardp CSS Certified Expert (Master Level) at Experts-Exchange Proud user of LITHIUM forum : CPU technology Webmaster of SEVEN-SEGMENTS : Elettronica per modellismo |
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
Quote:
o no? ![]()
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
|
![]() |
![]() |
![]() |
#4 | |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quote:
arr[1] è il contenuto del secondo elemento... arr[1] equivale a *(arr+1) &arr[1] eqivale a (arr+1) |
|
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
Quote:
ah sì ok ho capito....non mi rendevo conto dell'errore...ora però il compilatore mi dice "parse error at end of input" in corrisponmdenza di righe vuote dopo il codice...... che vol dire??
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
|
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Manca una grafa chiusa prima di system("PAUSE");
Le grafe mettile da sole in un raga...non come fai ora...altrimenti non ci capisci neinte... |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
ho provato a renderlo piu leggibile...così compila ma...uffffff non ne faccio uno giusto al primo colpo che balle!!!
ma concettualmente mi sembra giusto! ![]()
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Attento che la scanf con parametro %s legge una sola parola dall'input (mentre la printf stampa fino alla fine della stringa)...
Usa la gets al posto della scanf... |
![]() |
![]() |
![]() |
#9 | |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
Quote:
Grazie, mettendo gets (novità per quanto mi riguarda) il compilatore mi dice: too many arguments to function `gets' ![]() come mai?
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
|
![]() |
![]() |
![]() |
#10 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Perchè la gets serve solo per le stringhe...
gets(riga); |
![]() |
![]() |
![]() |
#11 | |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
Quote:
nel mio caso sono piu di una le stringhe...potrei assegnare ogni stringa ad una cella di un nuovo array...
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
|
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
cionci...
ma giuda stitico!!!!!! non ci riesco ma...ma... ma.... can del vacca!!! ufff ci sto smanettando su ancora ma ormai sono andato in balla! allora la procedura va ...almeno sono piuttosto convinto perche l'ho provata a parte.... ma per il resto meno di 0 secondo te puo andare come impostazione...come idea di fondo? -carico in un array tutta una stringa -con un puntatore inizio a scorrere l'array e passo ogni carattere in un altro array a[] , quando trovo uno spazio avvio la procedura piglatin sul contenuto dell array a che in teoria contiene la prima parola... -fatta "la traduzione" della prima parola....procedo scorrendo l'array iniziale dalla posizione "dopo" lo spazio....e riinizio a caricare l'array a[] che a cui ho riimpostato l'indice a 0... Codice:
#include <stdlib.h> #include <stdio.h> #define DIM 80 piglatin (char arr[], int dim) { char *p=&arr[0]; char *c; for (c=&arr[1]; c<=&arr[dim-1]; c++) printf ("%c", *c); printf ("%ca ",*p); } int main() { int g=0; char riga [DIM]; char a [DIM]; char *p; printf ("Scrivi una riga di testo\n"); scanf ("%s",&riga[0]); for (p=&riga[0]; p<&riga[DIM]; p++,g++) {if (*p!=' ') a[g]=*p; else{ p++; piglatin (a,g); g=0; } } system("PAUSE"); return 0; } ![]() provo a trovare un diverso approcio...
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#13 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
.
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#14 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
.
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#15 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
.
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#16 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
.
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#17 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
.
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#18 |
Senior Member
Iscritto dal: Jan 2002
Città: VENICE
Messaggi: 1241
|
Scusate ho fatto un disastro...
![]()
__________________
...Fight with your heart, and you're Destined For Glory But fight without soul and you will loose it all... |
![]() |
![]() |
![]() |
#19 | |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quote:
Altra soluzione fare una lettura carattere per carattere fino a quando viene immesso un \n o sono stati immessi 80 caratteri... In questo caso dovrai mettere manualmente lo \0 in fondo dopo l'ultimo carattere letto... |
|
![]() |
![]() |
![]() |
#20 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Secondo me stai facendo difficili le cose semplici...
Comunque ti ho corretto il codice... Codice:
#include <stdlib.h> #include <stdio.h> #define DIM 80 void piglatin (char arr[], int dim) { char *p=&arr[0]; char *c; for (c=&arr[1]; c<=&arr[dim-1]; c++) printf ("%c", *c); printf ("%ca ",*p); } int main() { int g=0; char riga [DIM]; char a [DIM]; char *p; printf ("Scrivi una riga di testo\n"); gets (riga); for (p=&riga[0]; p<&riga[DIM]; p++) { if (*p == '\0') { if(g > 0) piglatin(a, g); break; /*se raggiungo la fine della strina esco*/ } if (*p!=' ') a[g++]=*p; else { if(g > 0) piglatin (a,g); g=0; } } system("PAUSE"); return 0; } |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:55.