Você está na página 1de 6

Esercitazione 7

1) Esercizio 1
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<ctype.h>

#define N 40
int main()
{
char str[N], str2[N];
int num_number=0, num_alpha=0, num_other=0;
int i, j;
int flag;
printf("insert a string: ");
if(scanf("%s", str)==EOF){
printf("error while reading the string");
return -1;
}
printf("the length of the string is %d", strlen(str));
for(i=0;i<strlen(str);i++)
if(isdigit(str[i])!=0)
num_number++;
else if (isalpha(str[i]))
num_alpha++;
else
num_other++;
printf("\nstring contains %d numbers - %d alphabetic char - %d others", num_number, num_alpha,
num_other);
printf("\ninsert another string: ");
if(scanf("%s", str2)==EOF){
printf("error while reading the string");
return -2;
}
flag=1;
for(i=0; i <= strlen(str)-strlen(str2) && flag==1;i++){
flag=0;
for(j=0;j<strlen(str2) && flag==0;j++)
if(str[i+j]!=str2[j])
flag=1;
}
if (flag==1)
printf("the first string does not contain the second one");
else
printf("the first string contains the second one");

return 0;
}

2) Esercizio 2
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<ctype.h>
<string.h>

#define N 30
int main()
{
char str[N];
char* s;
int i, words=0;
s = gets(str);
if(s!=NULL) {
for(i=0; i<strlen(str); i++) {
if(isalnum(str[i])!=0) {
if(isupper(str[i])!=0)
str[i]=tolower(str[i]);
else if(isdigit(str[i])!=0)
str[i]='*';
}
else
str[i]='_';
}
}
puts(str);
for(i=0; i<strlen(str)-1; i++) {
if((str[i]=='_')&&(str[i+1]!='_'))
words++;
}
printf("\nIf we consider the \'_\' character as word separator there are is a total of %d words\n",
words+1);
return 0;
}

3) Esercizio 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 6
#define M 21+3+1
int main()

{
char time1[N], time2[N], result_string[M];
int h1, h2, m1, m2;
int t1, t2;
int distance;
printf("insert first time string: ");
scanf("%s", time1);
printf("insert second time string: ");
scanf("%s", time2);
if(sscanf(time1,"%d:%d", &h1, &m1)!=2 || (h1<0 || h1>23 || m1<0 || m1>59)){
printf("\n first string format is invalid");
return -1;
}
if(sscanf(time2,"%d:%d", &h2, &m2)!=2 || (h2<0 || h2>23 || m2<0 || m2>59)){
printf("\n second string format is invalid");
return -2;
}
t1 = h1*60+m1;
t2 = h2*60+m2;
if(t1<t2){
printf("\nt1 is minor than t2\n");
distance = t2 - t1;
sprintf(result_string,"<intervallo calcolato_%d>", distance);
puts(result_string);
}
else
printf("\nt1 is not minor than t2");
return 0;
}

4) Esercizio 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 61
#define M 5
int main()
{
char string[N];
char vett_str[M][N];
int num_words;
int i;
int sum = 0;
float avg;
int stat[N-1];
if(gets(string)==NULL) {

printf("\n a problem occurred while reading the sentence..");


return -1;
}
num_words = sscanf(string, "%s %s %s %s %s", vett_str[0], vett_str[1], vett_str[2], vett_str[3],
vett_str[4]);
printf("\n The string includes %d words", num_words);
for(i=0;i<N-1;i++)
stat[i]=0;
for(i=0 ; i < num_words ; i++){
sum += strlen(vett_str[i]); /* accumulate lengths */
stat[strlen(vett_str[i])]++;
}
avg = (float) sum/num_words;
printf("\n The length average is %.3f\n", avg);
for(i=0;i<N-1;i++){
if(stat[i]!=0)
printf("\n%d word(s) including %d character(s)", stat[i], i);
}
return 0;
}

5) Esercizio 5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 20
#define M 15
int main()
{
char tmp[N];
char str1[M], str2[M], str3[M], cmp[M];
int pr1, pr2, pr3, qnt;
printf("insert product 1 and its prize: ");
if (gets(tmp)==NULL) {
printf("Error in gets of product 1\n");
return -1;
}
if (sscanf(tmp,"%s %d", str1, &pr1)!=2) {
printf("Either product name or price of product 1 has not been inserted correctly\n");
return 1;
}
printf("insert product 2 and its prize: ");
if (gets(tmp)==NULL) {

printf("Error in gets of product 2\n");


return -2;
}
if (sscanf(tmp,"%s %d", str2, &pr2)!=2){
printf("Either product name or price of product 2 has not been inserted correctly\n");
return 2;
}
printf("insert product 3 and its prize: ");
if (gets(tmp)==NULL) {
printf("Error in gets of product 3\n");
return -3;
}
if (sscanf(tmp,"%s %d", str3, &pr3)!=2){
printf("Either product name or price of product 3 has not been inserted correctly\n");
return 3;
}
printf("insert a product to look for and a quantity: ");
if (gets(tmp)==NULL) {
printf("Error in gets of product that is being looked for\n");
return -4;
}
if (sscanf(tmp,"%s %d", cmp, &qnt)!=2) {
printf("Either product name or quantity of product that is being looked for, has not been
inserted correctly\n");
return 4;
}
if (strcmp(str1, cmp)==0){
printf("\n for %d units of product %s you have to pay %d", qnt, cmp, qnt*pr1);
}
else if (strcmp(str2, cmp)==0){
printf("\n for %d units of product %s you have to pay %d", qnt, cmp, qnt*pr2);
}
else if (strcmp(str3, cmp)==0){
printf("\n for %d units of product %s you have to pay %d", qnt, cmp, qnt*pr3);
} else{
printf("\n the product you digit is not available");
}
return 0;
}

6) Esercizio 6
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int car, old_car, flag_k=0;

do{
car = getchar(); /* press ctrl-Z to finish */
if(flag_k==1){
if(car=='h'){
putchar('k');
flag_k=0;
}
else if(car!='c'){ /* in case of double 'c' do not print it and check again for 'h'*/
putchar('c');
putchar(car);
flag_k=0;
}
}
else{
if(car=='c')
flag_k=1;
else if(old_car!=car)
putchar(car);
}
old_car = car;
}while(car!=EOF);
return 0;
}

Você também pode gostar