Managing Strings
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...
Determining whether the string is a palindrome
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.
How to do it…
- 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:
char str[80],rev[80];
- Enter characters that will be assigned to the str string:
printf("Enter a string: ");
scanf("%s",str);
- Compute the length of the string using the strlen...
Finding the occurrence of the first repetitive character in a string
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.
How to do it…
- 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:
char str1[80],str2[80];
- Enter characters to be assigned to str1. The characters...
Displaying the count of each character in a string
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.
How to do it…
- 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:
char str[80],chr[80];
- Prompt the user to enter a string. The entered string will be assigned to the str string:
printf("Enter a string: ");
scanf("%s",str);
- Compute the length of the string array, str, using strlen:
n=strlen(str);
- Define an integer array called count to...
Counting vowels and consonants in a sentence
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.
How to do it…
- Create a string array called str to input your sentence. As usual, the last character will be a null character:
char str[255];
- Define two variables, ctrV and ctrC:
int ctrV,ctrC;
- Prompt the user to enter a sentence of your choice:
printf("Enter a sentence: "...
Converting the vowels in a sentence to uppercase
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.