





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C program examples for finding the first, last, and all occurrences of a character in a given string, as well as programs to count character occurrences, find highest and lowest frequency characters, and remove characters from strings. These programs can be useful for practicing C programming and string manipulation.
Typology: High school final essays
1 / 9
This page cannot be seen from the preview
Don't miss anything!






#include <stdio.h> #include <string.h> #define MAX_SIZE 100 int indexOf(const char * str, const char toFind); int main() { char str[MAX_SIZE]; char toFind; int index; printf("Enter any string: "); gets(str); printf("Enter character to be searched: "); toFind = getchar(); index = indexOf(str, toFind); if(index == - 1 ) printf("'%c' not found.", toFind); else printf("'%c' is found at index %d.", toFind, index); return 0 ; } int indexOf(const char * str, const char toFind) { int i = 0 ; while(str[i] != '\0') { if(str[i] == toFind) return i; i++; } return - 1 ; }
#include <stdio.h> #define MAX_SIZE 100 int lastIndexOf(const char * str, const char toFind); int main() { char str[MAX_SIZE]; char toFind; int index;
printf("Enter any string: "); gets(str); printf("Enter any character to find: "); toFind = getchar(); index = lastIndexOf(str, toFind); printf("\nLast index of '%c' is %d", toFind, index); return 0 ; } int lastIndexOf(const char * str, const char toFind) { int index = - 1 ; int i = 0 ; while(str[i] != '\0') { // Update index if match is found if(str[i] == toFind) { index = i; } i++; } return index; }
#include <stdio.h> #define MAX_SIZE 100 int main() { char str[MAX_SIZE]; char toSearch; int i; printf("Enter any string: "); gets(str); printf("Enter any character to search: "); toSearch = getchar(); i= 0 ; while(str[i]!='\0') { /* If character is found in string */ if(str[i] == toSearch) { printf("'%c' is found at index %d\n", toSearch, i); } i++; } return 0 ; }
ascii = (int)str[i]; freq[ascii] += 1 ; i++; } max = 0 ; for(i= 0 ; i<MAX_CHARS; i++) { if(freq[i] > freq[max]) max = i; } printf("Maximum occurring character is '%c' = %d times.", max, freq[max]); return 0 ; }
#include <stdio.h> #define MAX_SIZE 100 #define MAX_CHARS 255 int main() { char str[MAX_SIZE]; int freq[MAX_CHARS]; int i = 0 , min; int ascii; printf("Enter any string: "); gets(str); for(i= 0 ; i<MAX_CHARS; i++) { freq[i] = 0 ; } i= 0 ; while(str[i] != '\0') { ascii = (int)str[i]; freq[ascii] += 1 ; i++; } min = 0 ; for(i= 0 ; i<MAX_CHARS; i++) { if(freq[i] != 0 ) { if(freq[min] == 0 || freq[i] < freq[min]) min = i; } } printf("Minimum occurring character is '%c' = %d.", min, freq[min]); return 0 ;
#include <stdio.h> #include <string.h> #define MAX_SIZE 100 int main() { char str[MAX_SIZE]; int i, len; int freq[ 26 ]; printf("Enter any string: "); gets(str); len = strlen(str); for(i= 0 ; i< 26 ; i++) { freq[i] = 0 ; } for(i= 0 ; i<len; i++) { if(str[i]>='a' && str[i]<='z') { freq[str[i] - 97 ]++; } else if(str[i]>='A' && str[i]<='Z') { freq[str[i] - 65 ]++; } } printf("\nFrequency of all characters in the given string: \n"); for(i= 0 ; i< 26 ; i++) { if(freq[i] != 0 ) { printf("'%c' = %d\n", (i + 97 ), freq[i]); } } return 0 ; }
#include <stdio.h> #include <string.h> #define MAX_SIZE 100 void removeFirst(char *, const char);
i= 0 ; while(i<len) { if(str[i] == toRemove) { lastPosition = i; } i++; } if(lastPosition != - 1 ) { i = lastPosition; while(i<len) { str[i] = str[i+ 1 ]; i++; } } }
#include <stdio.h> #include <string.h> #define MAX_SIZE 100 void removeAll(char *, const char); int main() { char str[MAX_SIZE]; char toRemove; printf("Enter any string: "); gets(str); printf("Enter character to remove from string: "); toRemove = getchar(); removeAll(str, toRemove); printf("String after removing '%c': %s", toRemove, str); return 0 ; } void removeAll(char * str, const char toRemove) { int i, j; int len = strlen(str); for(i= 0 ; i<len; i++) { if(str[i] == toRemove) { for(j=i; j<len; j++) { str[j] = str[j+ 1 ]; }
len--; i--; } } }
#include <stdio.h> #define MAX_SIZE 100 void removeDuplicates(char * str); void removeAll(char * str, const char toRemove, int index); int main() { char str[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("String before removing duplicates: %s\n", str); removeDuplicates(str); printf("String after removing duplicates: %s\n", str); return 0 ; } void removeDuplicates(char * str) { int i = 0 ; while(str[i] != '\0') { removeAll(str, str[i], i + 1 ); i++; } } void removeAll(char * str, const char toRemove, int index) { int i; while(str[index] != '\0') { if(str[index] == toRemove) { i = index; while(str[i] != '\0') { str[i] = str[i + 1 ]; i++; } } else { index++; } }