Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercises on String Operations

Similar presentations


Presentation on theme: "Exercises on String Operations"— Presentation transcript:

1 Exercises on String Operations

2 Exercise 1: Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. #include <stdio.h> #include <stdlib.h> int main(void){ char x[10]; char y[10]; char z[10]; char t[10]; int sum=0; puts("Enter 4 strings to be summed"); scanf(" %s %s %s %s", x, y, z, t); printf("\nSum is=%d", atoi(x)+atoi(y)+atoi(z)+atoi(t)); }

3 Exercise 2: Write a program that Counts the Number of Words in a String
scanf with %s only considers one word scanf(“%s”, s); #include <stdio.h> #include <ctype.h> #include <string.h> int main(){ char s [40],ch; int i,c=0; printf("Enter any string : "); for(i=0;ch!='\n';i++){ scanf("%c",&ch); s[i]=ch; } s[i]='\0'; printf("%s", s); int len = strlen(s); for(i=0;i<len;i++){ if(isspace(s[i])) { c++;} } printf("\n\nTotal words are %d",c+1); return 0; } To read a String with spaces fgets (s, 40, stdin);


Download ppt "Exercises on String Operations"

Similar presentations


Ads by Google