Strings are nothing but arrays that store characters. Since strings are character arrays, they utilize less memory and lead to efficient object code, making programs run faster. Just like numerical arrays, strings are zero-based, that is, the first character is stored at index location 0. In C, strings are terminated by a null character, \0.
The recipes in this chapter will enhance your understanding of strings and will acquaint you with string manipulation. Strings play a major role in almost all applications. You will learn how to search strings (which is a very common task), replace a string with another string, search for a string that contains a specific pattern, and more.
In this chapter, you will learn how to create the following recipes using strings:
- Determining whether the string is a palindrome
- Finding the occurrence...
A palindrome is a string that reads the same regardless of whether it is in a forward or backwards order. For example, the word
radar is a palindrome because it reads the same way forwards and backwards.
- Define two 80-character strings called str and rev(assuming your string will not exceed 79 characters). Your string can be of any length, but remember that the last position in the string is fixed for the null character \0:
- Enter characters that will be assigned to the str string:
printf("Enter a string: ");
- Compute the length of the string using the strlen...
In this recipe, you will learn how to create a program that displays the first character to be repeated in a string. For example, if you enter the string racecar, the program should give the output as The first repetitive character in the string racecar is c. The program should display No character is repeated in the string if a string with no repetitive characters is entered.
- Define two strings called str1 and str2. Your strings can be of any length, but the last position in the string is fixed for the null character \0:
- Enter characters to be assigned to str1. The characters...
In this recipe, you will learn how to create a program that displays the count of each character in a string in a tabular form.
- Create a string called str. The last element in the string will be a null character, \0.
- Define another string called chr of matching length, to store the characters of str:
- Prompt the user to enter a string. The entered string will be assigned to the str string:
printf("Enter a string: ");
- Compute the length of the string array, str, using strlen:
- Define an integer array called count to...
In this recipe, you will learn how to count the number of vowels and consonants in an entered sentence. The vowels are a, e, i, o, and u, and the rest of the letters are consonants. We will use ASCII values to identify the letters and their casing:
The blank spaces, numbers, special characters, and symbols will simply be ignored.
- Create a string array called str to input your sentence. As usual, the last character will be a null character:
- Define two variables, ctrV and ctrC:
- Prompt the user to enter a sentence of your choice:
printf("Enter a sentence: "...
In this recipe, you will learn how to convert all of the lowercase vowels in a sentence to uppercase. The remaining characters in the sentence, including consonants, numbers, special symbols, and special characters, are simply ignored and will be left as they are.
Converting the casing of any letter is done by simply changing the ASCII value of that character, using the following formulas:
- Subtract 32 from the ASCII value of a lowercase character to convert it to uppercase
- Add 32 to the ASCII value of an uppercase character to convert it to lowercase
The following diagram shows the ASCII values of the uppercase and lowercase vowels:
The ASCII value of the uppercase letters is lower than that of lowercase letters, and the difference between the values is 32.