Chapter:
Strings-in-C-programming-
1. Write a C program to count occurrences of a word in a given string.
/** * C program to count occurrences of a word in a given string */ #include#include #define MAX_SIZE 100 // Maximum string size /* Function declaration */ int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; int count; /* Input string and word from user */ printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(toSearch); count = countOccurrences(str, toSearch); printf("Total occurrences of '%s': %d", toSearch, count); return 0; } /** * Get, total number of occurrences of a word in a string */ int countOccurrences(char * str, char * toSearch) { int i, j, found, count; int stringLen, searchLen; stringLen = strlen(str); // length of string searchLen = strlen(toSearch); // length of word to be searched count = 0; for(i=0; i <= stringLen-searchLen; i++) { /* Match word with string */ found = 1; for(j=0; j
2. Write a C program to find total number of vowels and consonants in a string.
Without using pointers:
/** * C program to count total number of vowel or consonant in a string using switch case */ #include#include #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; int i, len, vowel, consonant; /* Input strings from user */ printf("Enter any string: "); gets(str); vowel = 0; consonant = 0; len = strlen(str); for(i=0; i ='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) { switch(str[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': vowel++; break; default: consonant++; } } } printf("Total number of vowel = %d\n", vowel); printf("Total number of consonant = %d\n", consonant); return 0; }
Using Pointers:
/** * C program to count total number of vowel or consonant in a string using pointers */ #include#include #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char * s = str; int vowel, consonant; /* Input strings from user */ printf("Enter any string: "); gets(str); vowel = 0; consonant = 0; while(*s) { if((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <='Z')) { switch(*s) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': vowel++; break; default: consonant++; } } s++; } printf("Total number of vowel = %d\n", vowel); printf("Total number of consonant = %d\n", consonant); return 0; }
3. Write a C program to find maximum occurring character in a string.
/** * C program to find maximum occurring character in a string */ #include#define MAX_SIZE 100 // Maximum string size #define MAX_CHARS 255 // Maximum characters allowed int main() { char str[MAX_SIZE]; int freq[MAX_CHARS]; // Store frequency of each character int i = 0, max; int ascii; printf("Enter any string: "); gets(str); /* Initializes frequency of all characters to 0 */ for(i=0; i freq[max]) max = i; } printf("Maximum occurring character is '%c' = %d times.", max, freq[max]); return 0; }
4. Write a C program to find length of a string.
Without using pointer:
#include#define MAX_SIZE 100 // Maximum size of the string int main() { char text[MAX_SIZE]; /* Declares a string of size 100 */ int i; int count= 0; /* Input a string from user */ printf("Enter any string: "); gets(text); /* Iterate till the last character of string */ for(i=0; text[i]!='\0'; i++) { count++; } printf("Length of '%s' = %d", text, count); return 0; }
Using pointer:
/** * C program to find length of a string using pointer */ #include#define MAX_SIZE 100 // Maximum size of the string int main() { char text[MAX_SIZE]; /* Declares a string of size 100 */ char * str = text; /* Declare pointer that points to text */ int count = 0; /* Input string from user */ printf("Enter any string: "); gets(text); /* Iterate though last element of the string */ while(*(str++) != '\0') count++; printf("Length of '%s' = %d", text, count); return 0; }
5. Write a C program to compare two strings
Without using pointer:
/** * C program to compare two string without using string library functions */ #include#define MAX_SIZE 100 // Maximum string size /* Compare function declaration */ int compare(char * str1, char * str2); int main() { char str1[MAX_SIZE], str2[MAX_SIZE]; int res; /* Input two strings from user */ printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); /* Call the compare function to compare strings */ res = compare(str1, str2); if(res == 0) { printf("Both strings are equal."); } else if(res < 0) { printf("First string is lexicographically smaller than second."); } else { printf("First string is lexicographically greater than second."); } return 0; } /** * Compares two strings lexicographically. * Returns 0 if both strings are equal, * negative if first string is smaller * otherwise returns a positive value */ int compare(char * str1, char * str2) { int i = 0; /* Iterate till both strings are equal */ while(str1[i] == str2[i]) { if(str1[i] == '\0' && str2[i] == '\0') break; i++; } // Return the difference of current characters. return str1[i] - str2[i]; }
Using Pointers:
/** * C program to compare two string without using string library functions */ #include#define MAX_SIZE 100 // Maximum string size /* Compare function declaration */ int compare(char * str1, char * str2); int main() { char str1[MAX_SIZE], str2[MAX_SIZE]; int res; /* Input two strings from user */ printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); /* Call the compare function to compare strings */ res = compare(str1, str2); if(res == 0) { printf("Both strings are equal."); } else if(res < 0) { printf("First string is lexicographically smaller than second."); } else { printf("First string is lexicographically greater than second."); } return 0; } /** * Compares two strings lexicographically. */ int compare(char * str1, char * str2) { while((*str1 && *str2) && (*str1 == *str2)) { str1++; str2++; } return *str1 - *str2; }
6. Write a program in C to find largest and smallest word in a string.
#include#include #include void main() { char str[100], word[20], mx[20], mn[20], c; int i = 0, j = 0, flg = 0; printf("\n\nFind the largest and smallest word in a string :\n"); printf("Input the string : "); i = 0; do { fflush(stdin); c = getchar(); str[i++] = c; } while (c != '\n'); str[i - 1] = '\0'; for (i = 0; i < strlen(str); i++) { while (i < strlen(str) && !isspace(str[i]) && isalnum(str[i])) { word[j++] = str[i++]; } if (j != 0) { word[j] = '\0'; if (!flg) { flg = !flg; strcpy(mx, word); strcpy(mn, word); } if (strlen(word) > strlen(mx)) { strcpy(mx, word); } if (strlen(word) < strlen(mn)) { strcpy(mn, word); } j = 0; } } printf("The largest word is '%s' \nand the smallest word is '%s' \nin the string : '%s'.\n", mx, mn, str); }
7. Write a C program to copy one string to anor string.
Without using pointer:
/** * C program to copy one string to another string using while loop */ #include#define MAX_SIZE 100 // Maximum size of the string int main() { char text1[MAX_SIZE]; char text2[MAX_SIZE]; int i; /* Input string from user */ printf("Enter any string: "); gets(text1); /* Copy text1 to text2 character by character */ i=0; while(text1[i] != '\0') { text2[i] = text1[i]; i++; } //Makes sure that the string is NULL terminated text2[i] = '\0'; printf("First string = %s\n", text1); printf("Second string = %s\n", text2); printf("Total characters copied = %d\n", i); return 0; }
Using pointer:
/** * C program to copy one string to another string using pointer */ #include#define MAX_SIZE 100 // Maximum size of the string int main() { char text1[MAX_SIZE], text2[MAX_SIZE]; char * str1 = text1; char * str2 = text2; /* Input string from user */ printf("Enter any string: "); gets(text1); /* Copy text1 to text2 character by character */ while(*(str2++) = *(str1++)); printf("First string = %s\n", text1); printf("Second string = %s\n", text2); return 0; }
8. Write a C program to concatenate two strings.
Without using Pointer:
/** * C program to concatenate two strings */ #include#define MAX_SIZE 100 // Maximum string size int main() { char str1[MAX_SIZE], str2[MAX_SIZE]; int i, j; /* Input two strings from user */ printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); /* Move till the end of str1 */ i=0; while(str1[i] != '\0') { i++; } /* Copy str2 to str1 */ j = 0; while(str2[j] != '\0') { str1[i] = str2[j]; i++; j++; } // Make sure that str1 is NULL terminated str1[i] = '\0'; printf("Concatenated string = %s", str1); return 0; }
Using Pointer:
/** * C program to concatenate two strings using pointer */ #include#define MAX_SIZE 100 // Maximum string size int main() { char str1[MAX_SIZE], str2[MAX_SIZE]; char * s1 = str1; char * s2 = str2; /* Input two strings from user */ printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); /* Move till the end of str1 */ while(*(++s1)); /* Copy str2 to str1 */ while(*(s1++) = *(s2++)); printf("Concatenated string = %s", str1); return 0; }
9. Write a C program to search all occurrences of a character in a string.
/** * C program to search all occurrences of a character in a string */ #include#define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char toSearch; int i; /* Input string and character to search from user */ printf("Enter any string: "); gets(str); printf("Enter any character to search: "); toSearch = getchar(); /* Run loop till the last character of string */ 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; }
All Chapters
View all Chapter and number of question available From each chapter from C-programming-
C Programs on integers
C Programs with explanation and detailed solution and output for practising and improving your coding skills, suitable for any level learner..
C programs on various integers
C programming tutorials Coming soon...
Learn and master the C Programming concepts with IOEBOOSTER, suitable for both beginners and intermediate level students as per IOE and POU syllabus.These c programming tutorials are completely free and important for exam point of view and for mast..
Recursive program in c
Recursion in computer science is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. Such problems can generally be solved by iteration, but this needs to identify and index the smaller instances at programming time.
Arrays
- c programms on arrays
- complete IOE solution on arrays
- Detailed description
- And much more
Strings in C programming
- c programms on Strings
- complete IOE solution on Strings
- Detailed description
- Each one using pointer Concepts
- And much more
Data Files
- C programming concepts of data files and data structures
- IOE C Programming Solutions
- Important Numerous C Program
Pattern Printing in C
Learn to print half pyramid, pyramid, inverted pyramid, Pascal's Triangle Floyd's triangle and various pattern in C Programming using control statements. This helps you to build up your logic towards C Programming Problems.
Topics
This Chapter Strings-in-C-programming- consists of the following topics
Guest