Usando il prototipo int unisci_stringhe (char s1[] , char s2[] , char s3[]) scrivere un programma che acquisisca due stringhe contenenti varie parole separate da spazio.
La funzione deve ricopiare nella stringa destinazione s3 la seconda parola di s1 e la terza si s2.
la funzione restituisce come valore la lunghezza di s3.
Il programma da me inserito(non funzionante ovviamente) è il seguente:
#include <stdio.h>
#include <stdlib.h>
main(){
int risultato=0;
char p[30], q[30],k[30];
int unisci_stringhe (char s1[] , char s2[] , char s3[]);
printf("Inserisci prima stringa\n");
scanf("%s", p);
system("PAUSE");
printf("Inserisci seconda stringa\n");
scanf("%s", q);
system("PAUSE");
risultato= unisci_stringhe(p ,q , k);
printf("%d\n",risultato);
system("PAUSE");
}
int unisci_stringhe (char s1[] , char s2[] , char s3[]){
int i=0, j=0;
int p=0,k=0,z=0,l=0, lunghezza;
int contas1=0 , contas2=0;
while(s1[i]!='\0'){
if(i>0){
if(s1[i]==' ' && s1[i-1] !=' ')
{ contas1 ++;}
if(contas1==1)
{k=i+1; p=1;
while(s1[i]!=' ' || s1[i]!='\0'){
s3[p]=s1[i];
k++;
p++;
}
}
}
i++;
}
while(s2[j]!='\0'){
if(j>0){
if(s2[j]==' ' && s2[j-1] !=' ')
{contas2 ++;}
if(contas2==2)
{z=j+1;
while(s2[z]!=' ' || s2[z]!='\0'){
s3[p]=s2[z];
p++;
z++;
}
}
}j++;
}
s3[p+1]='\0';
return p;
}