1.calculates and prints the number of and sum of the integers between 1 and 100 (inclusive) that are not divisible by 3 or 5
codes:
- #include<stdio.h>
- int main(){
- int i;
- int sum=0;
- int count=0;
- for(i=1;i<=100;i++){
- if(i%3!=0 && i%5!=0){
- sum=sum+i;
- count=count+1;
- }
- }
- printf("sum=%d number=%d\n",sum,count);
- return 0;
- }
2.The program prompts for a string (max 80 characters) and converts uppercase characters to lowercase
codes:
- #include <stdio.h>
- #include <string.h>
-
- int main(void) {
- char str[81];
- int i, len;
- printf("Enter string:\n");
- fgets(str, 81, stdin);
- len = strlen(str);
- if(str[len-1] == '\n') {
- str[len-1] = 0;
- }
- for(i=0; str[i]; i++) {
- if(str[i] >= 'A' && str[i] <= 'Z') {
- str[i] += 32;
- }
- }
- printf("%s\n", str);
- return 0;
- }
3.Convert lowercase characters to uppercase instead.
codes:
- #include <stdio.h>
- #include <string.h>
-
- int main(void) {
- char str[81];
- int i, len;
- printf("Enter string:\n");
- fgets(str, 81, stdin);
- len = strlen(str);
- if(str[len-1] == '\n') {
- str[len-1] = 0;
- }
- for(i=0; str[i]; i++) {
- if(str[i] >= 'a' && str[i] <= 'z') {
- str[i] -= 32;
- }
- }
- printf("%s\n", str);
- return 0;
- }
4.Count the number of each type of letter (A to Z) and print a table showing these counts. Ignore non-alphabetic characters.
codes:
- #include <stdio.h>
- #include <string.h>
-
- int main(void) {
- char str[81];
- int lettercount[26];
- int i, len;
-
- for(i=0; i<26; i++) {
- lettercount[i] = 0;
- }
-
- printf("Enter string:\n");
- fgets(str, 81, stdin);
-
- len = strlen(str);
-
- if(str[len-1] == '\n') {
- str[len-1] = 0;
- }
-
- for(i=0; str[i]; i++) {
- if(str[i] >= 'A' && str[i] <= 'Z') {
- lettercount[str[i]-'A']++;
- } else if(str[i] >= 'a' && str[i] <= 'z') {
- lettercount[str[i]-'a']++;
- }
- }
-
- for(i=0; i<26; i++) {
- printf("%c: %d\n", 'A'+i, lettercount[i]);
- }
- return 0;
- }
5.Accept 20 lines of text (each up to 80 characters) instead of 1 line, then print a count of each letter seen (the total over all lines).
codes:
- #include <stdio.h>
- #include <string.h>
-
- int main(void) {
- char str[81];
- int lettercount[26];
- int i, len;
- int linecount;
-
- for(i=0; i<26; i++) {
- lettercount[i] = 0;
- }
-
- for(linecount=0; linecount<20; linecount++) {
- printf("Enter string:\n");
- fgets(str, 81, stdin);
-
- len = strlen(str);
-
- if(str[len-1] == '\n') {
- str[len-1] = 0; /* Null character - we could also have said '\0' */
- }
-
- for(i=0; str[i]; i++) {
- if(str[i] >= 'A' && str[i] <= 'Z') {
- lettercount[str[i]-'A']++;
- } else if(str[i] >= 'a' && str[i] <= 'z') {
- lettercount[str[i]-'a']++;
- }
- }
- }
-
- for(i=0; i<26; i++) {
- printf("%c: %d\n", 'A'+i, lettercount[i]);
- }
- return 0;
- }
6.As 5, but the program accepts any number of lines of text, up to when it sees a line containing only the text “END” (without the quotes)
codes:
- #include <stdio.h>
- #include <string.h>
-
- int main(void) {
- char str[81];
- int lettercount[26];
- int i, len;
- int finished=0;
- for(i=0; i<26; i++) {
- lettercount[i] = 0;
- }
-
- while(!finished) {
-
- printf("Enter string:\n");
- fgets(str, 81, stdin);
-
-
- len = strlen(str);
-
-
- if(str[len-1] == '\n') {
- str[len-1] = 0;
- }
-
-
- if(strcmp(str, "END") == 0) {
-
- break;
- }
-
-
- for(i=0; str[i]; i++) {
- if(str[i] >= 'A' && str[i] <= 'Z') {
- lettercount[str[i]-'A']++;
- } else if(str[i] >= 'a' && str[i] <= 'z') {
- lettercount[str[i]-'a']++;
- }
- }
- }
-
- for(i=0; i<26; i++) {
- printf("%c: %d\n", 'A'+i, lettercount[i]);
- }
- return 0;
- }