Follow Me

String exercises in C Programming in C

  • Convert an hexadecimal string to a number: Write a program that reads a string and interprets it as a positive integer in the hexadecimal base. For the conversion, use the functions of <ctype.h> and the alphabetic precedence of the characters. The conversion ignores characters that do n...
  • Convert an integer to words: Write a program that will prompt for and read a positive integer less than 1000from the keyboard, and then create and output a string that is the value of the integer in words. For example, if 941 is entered, the program will create the string "Nine ...
  • Analyze comma-separated list of words: Write a program that will allow a list of words to be entered separated by commas,and then extract the words and output them one to a line, removing any leading or trailingspaces. For example, if the input isJohn , Jack , Jillthen the output will beJ...
  • Removing empty spaces from a string (using gets to read the string): Write a program that reads a string and removes emtpy spaces. Use function gets to read the string....
  • Removing empty spaces from a string ended by a dot (using scanf to read the string): Write a program that reads a string that ends with a dot and removes emtpy spaces. Use function scand to read the string....
  • Check whether 2 strings are identical or not: Write a program that checks whether 2 strings are identical or not. ...
  • Count the number of words in a sentence: Write a program that reads a string using gets and then counts and displays the number of words....
  • Append the character '-' between the characters of a word: Write a program that reads a word and then appends the character '-' between the characters. Example:Enter a word : antounWith '-' between each letter : a-n-t-o-u-n...
  • Display a substring from a string: Write a program in that display a substring from a given string knowing its length and its starting position. ExampleEnter the string : my name is totoEnter the position : 4Enter the length of substring : 6The substring retrieved from the string begi...
  • Checks if a substring is present in a string: Write a program that checks whether a given substring is present in the given string. ExampleEnter the string : my name is totoInput the substring : toThe substring exists in the string at position 12....
  • Longest and smallest word in a string: Write a program that finds the first longest and smallest word in a string....
  • Split a string by space into words: Write a program that splits a string by space into words. Use an array of strings to store the words...
  • Encoding and decoding a string (version 1): Write a program to encode text and to decode the encoded text. Perfrom the encoding so that every character is replaced by its next character. For example reaplce a by b, b by c and so on. Replace z by a. Example:plain text: ProgramEncoded text: Qsph...
  • Encoding and decoding a string (version 2): Write a program to encode text and to decode the encoded text. Perfrom the encoding saccording to these replacements \(\begin{array}{c c c c c c c c c c c c c c c c c c c c c c c c c c} a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z \\ m&n&k&g&h&d&t&a&b&w&v&u&p&r&q&c&z&j&x&i&e&y&f&l&o&s\end{array}\)...
  • Placing characters in alphabetical order: Input a string and change it is that the characters are placed in alphabetical order. For example, the string "motivate" should be changed to "aeimottv"...
  • Replace successive occurrences of the same character with a single character occurrence: Write a program that displays a string of characters on the screen but replaces all successive occurrences of the same character with a single character occurrence. For example, the string "aaabcbbccddd" is displayed "abcbcd". ...
  • Function sorting an array of strings: Write a program that reads 10 words and stores them in an array of strings. write a function that sorts the 10 words lexicographically using the strcmp and strcpy functions. Display the sorted array....
  • Hash code function: Define the function hash_code, which returns the hash code of a string.    (hash_code ("Abc") = ASCII(A) + ASCII(b) + ASCII(c) = 65 + 98+ 99 = 262 )...
  • Displaying numbers in blue and vowels in red: Define the function IsNumeric which returns 1 if the character is numeric, 0 otherwise Define the function IsVowel which returns 1 if the character is a vowel, 0 otherwise (Vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}). Then, write a progr...
  • File names: In this exercise we consider strings denoting complete file names. A complete file name consists of 2 parts: a name and an extension (example: prog.c is a file name, where “prog” is the name, and “c” is the extension). Therefore, a file name ...
  • Check if a string S1 is a substring of a string S2 using arrays: Write the function OnlyCapitals that takes a string S and checks if it contains only capital letters. The function returns 1 if the S contains only capital letters, 0 otherwise. Write the function CountCapitalLetters that takes a string S and an arra...
  • Regrouping alphabets in strings: Write the function isAlphabet that checks if a character passed as parameter is an alphabet or not (the function returns 1 if the character is an alphabet or 0 otherwise). Write the function EliminationOfNoAlphabet that takes a string S as parameter ...
  • Replace the most frequent capital letter by the least frequent capital letter in a string: Write the function isCapital that takes a string and returns 1 if the string contains only (exclusively) capital alphabet, and 0 otherwise. Write the function Replace that takes a string and replaces the most frequent capital letter by the least freq...
  • Append the same string: Write a program that reads a string \(\texttt{STR}\) from the keyboard and then adds the reverse of \(\texttt{STR}\) to its end (i.e. \(\texttt{STR}\) should contain the original string and its reverse).You may assume that the size of the string \(\texttt{STR}\)...
  • Appending 2 strings in a third: Write a program that reads two strings STR1 and STR2, then appends them in a third string STR3 without using any predefined function from the string.h library.Each of the 3 strings can contain 20 characters at maximum.The program should test if the t...
  • Display name and birth date: Write a program that reads and displays your first name and date of birth on two different lines....
  • Display 6 words in reverse order: Write a program that reads 6 words, separated by spaces and then displays them in a line, but in reverse order. The words are stored in 6 variables M1, ..., M6. Example:here is a small sentence !! sentence small a is here...
  • Calculate length - number of occurrence - reverse a string: Write a program that reads a line of text (not exceeding 200 characters) stores it in a TXT variable and then displays: the length L of the string. the number of 'e' contained in the string. the whole sentence backwards, without changing the content ...
  • Remove a character from a string: Write a program that reads a text TXT (less than 200 characters) and removes all occurrences of the 'e' character by packing the remaining items. The changes will be made in the same TXT variable. Example   This line contains some letters e.   Th...
  • Length of a full name (using 2 strings): Write a program that asks for the user's last name and first name and then displays the total length of the name without counting spaces. Use the strlen function. Example:    Enter your first and last name : Mickey Mouse    Hello Mickey Mouse!...
  • Length of a full name (using 1 string): Write a program that asks for the user's last name and first name and then displays the total length of the name without counting spaces. Use the strlen function. Example:    Enter your first and last name : Mickey Mouse    Hello Mickey Mouse!...
  • Compare 2 strings using strcmp: Write a program that reads two strings CH1 and CH2, compares them lexicographically and displays the result using strcmp: Example:    Enter the first string : ABC    Enter the second string: abc    "ABC" precedes "abc"...
  • Compare 2 strings without using strcmp: Write a program that reads two strings CH1 and CH2, compares them lexicographically and displays the result without using strcmp: Example:    Enter the first string : ABC    Enter the second string: abc    "ABC" precedes "abc"...
  • Merging half of strings using the string library: Write a program that reads two strings CH1 and CH2 and copies the first half of CH1 and the first half of CH2 into a third CH3. Show the result. Use the special functions of the string library....
  • Merging half of strings using gets and puts functions: Write a program that reads two strings CH1 and CH2 and copies the first half of CH1 and the first half of CH2 into a third CH3. Show the result. Use only the gets and puts functions....
  • Sort an array of strings: Write a program that reads 10 words and stores them in an array of strings. Sort the 10 words lexicographically using the strcmp and strcpy functions. Display the sorted array....
  • Day of the week: Write a program that reads a number between 1 and 7 and displays the name of the corresponding day of the week: "monday" for 1 "tuesday" for 2 . . . "sunday" for 7 Use the first element of the array to memorize a small error message....
  • Reverse a string using an array of strings: Write a program that reads 5 words, separated by spaces and then displays them in a line, but in reverse order. The words are stored in an array of strings....
  • Compare 2 strings: Write a program that reads two strings of characters, and indicates their lexicographic precedence in the character code of the machine (here: ASCII code)....
  • Uppercase to lowercase and vice versa: Write a program that reads a string of characters STR and converts all uppercase letters to lowercase and vice versa. The result will be stored in the same STR variable and displayed after the conversion....
  • Convert a string to a number: Write a program that reads a string of characters and interprets it as a positive integer in the decimal base. For the conversion, use the functions of <ctype.h> and the alphabetic precedence of the characters from '0' to '9'. Save the result i...
  • Reverse and squeeze empty spaces in a string: Write a program that reads a string s and copy s into a string t (of the same size) in reverse order, except that a sequence of white space is squeezed to a single space....
  • Number of occurrence of a string into another one: Write a program that reads two strings s1 and s2, and then prints on the screen the number of times s1 appears in s2, and the position of each occurrence of s1 in s2....
  • Number of occurrence of a character in a string: Write an algorithm that reads a string s and a character c, prints on the screen the number of times c appears in s and the first position of c in s....
  • Looking for palindromes: A palindrome is a phrase that reads the same backward as forward, ignoring whitespace and punctuation. For example, “Madam, I’m Adam” and “Are we not drawn onward, we few? Drawn onward to new era?” are palindromes. Write a program that will...
  • Delete the first occurrence of a string in an another string: Write a program that deletes the first occurrence of an DEL string in a STR string. Examples: deleting the first occurrence of 'PHON' from 'ALPHONSE gives 'ALSE' deleting the first occurrence of 'EI' from 'PIERRE gives 'PIERRE' deleting the first occ...
  • Replace the first occurrence of a string in an another string: Write a program that replaces the first occurrence of a string STR1 with the string STR 2 in a string STR. Use an END string to save the string during the replacement. Examples: replacing the first occurrence of 'PHON' by 'OY' in 'ALPHONSE' gives 'AL...
  • Replace all occurrences of a string in an another string: Write a program that replaces all occurrences of a string STR1 with the string STR2 in a string STR. Use a END to save the string during the replacement. Examples: replacing all occurrences of 'PHON' by 'OY' in 'ALPHONSE' gives 'ALOYSE' replacing all...
  • Length of a string using an iterative function: Write the function LENGTH which returns the length of a string of characters CH as result....
  • Converts all letters in a string to uppercase using an iterative function: Write the UPPER function that converts all letters in a string to uppercase....
  • Concatenate two strings using an iterative function: Write the function ADD having as parameters STR1 and STR2 which copies the string STR2 to the end of the string STR1....
  • Reverse a string using an iterative function: Write the REVERSE function which reverses the character order of a string....
  • Number of words in a string using an iterative function: Write the function NWORDS which returns as result the number of words contained in a string of characters. Use a logical variable, the function isspace and a helper variable N....
  • Check whether the first N characters of two strings are equal using an iterative function: Write the function EQUAL_N which returns the value 1 if the first N characters of STR1 and STR2 are equal, otherwise the value 0. (If N is greater than the length of STR1 or STR2, the result can be 1 or 0)....
  • Encryption of a character: Write a function \(\texttt{char encrypt (char c, int k)}\) that encrypts a character as follows: if c is an alphabetic character, shift c by k to the right (+ k) otherwise shift c by k to the left (-k). Write a main function that tests the function....
  • Displaying digits in words: Write a function digitname that takes a digit and displays its name in uppercase letters; for example if the digit is 4, the function displays FOUR. Write a C program that reads a positive integer n, and calls the above function to display the digits...
  • Number of words separated by a space: Write a function called nbrofWords that has an array of characters as parameter (string). This function must return the number of words in this string without considering spaces. We suppose that the string is composed of words separated by single bla...
  • Encryption of digits: The purpose of this application is to encrypt an integer number into a text message (string).  The following replacements of digits should be made in the encryption process: $$0\rightarrow ’a’, 1 \rightarrow ’b’, 2 \rightarrow ’c’, 3 \ri...
  • Checking palindrome words using functions: The objective of this exercise is to propose a new algorithm to check if a word (string without spaces) is palindrome. Write the function "firstlast" that takes as parameter a word (a string without spaces) and construct a new string by regrouping th...
  • Removing a sequence of 3 characters from a string: Write a function Reduction that takes a string S and eliminates from S every sequence of 3 or more consecutive occurrences of the same character. The function returns 1 if it finds at least one sequence to eliminate from S and 0 if no sequence to eli...
  • Program output 7: Give the output on the screen of the following program #include <stdio.h> #define PRINT(format,x) printf(#x " = %"#format"\n",x) int main(){     int integer = 5;     char character = '5';     char *string = "5";     PRINT(d, string)...

Back to the list of exercises