Common examples of string in C

July 5, 2017 3068 This post is an extension of the ‘string chapter’ of the C course. If you need to learn basics then visit the C course first. You can also practice a good number of question from the practice section.

Sorting strings in lexicographical order (dictionary order)

We will use bubble sort to sort the given strings in lexicographical order. We will first make a 2D array of characters and then implement bubble sort on it. You can read a post on bubble sort if you are not familiar with it.

#include <stdio.h>
#include <string.h>

int main()
{
int i,j;
char s;

//taking input
for(i=0; i<10; i++)
{
scanf("%s",s[i]);
}

//implementing bubble sort
for(j=0; j<10; j++)
{
//initially swapped is false
int swapped = 0;
i = 0;
while(i<9)
{
//comparing the adjacent elements
if(strcmp(s[i],s[i+1]) > 0)
{
//swapping
char temp;
strcpy(temp, s[i]);
strcpy(s[i], s[i+1]);
strcpy(s[i+1], temp);
//changing the value of swapped to 1
swapped = 1;
}
i++;
}
//if swapped is false then the array is sorted
//we can stop the loop
if (!swapped)
break;
}

//printing sorted strings
printf("\nAfter sorting\n");
for(i=0; i<10; i++)
{
printf("%s\n",s[i]);
}

return 0;
}

We are using bubble sort to sort the strings and then using the predefined C functions like strcpy and strcmp from the string.h library to copy and compare the strings respectively.

Checking whether two strings are anagrams or not

Two strings are said to be anagrams of each other if they are made of same characters i.e., if we can rearrange the characters of one string to form the another string then they are anagrams of each other. For example, ‘ ajax’ and ‘ xaja’ are anagrams of each other.

To check whether two strings are anagrams of each other or not, we just need to check if they contain same characters or not and to do so we can make two arrays of 26 elements (each element representing an alphabet) and store the frequency of the characters in those arrays. For example, if a string is ‘abd’ then the array will be {1, 1, 0, 1, 0, 0, ……, 0}. The first element of the array represents the character ‘a’ and is 1 since ‘a’ has appeared one time in the string. Similarly, the third element of the array (representing ‘c’) is 0 because there is no ‘c’ in the string ‘abd’. At last, we can compare both the arrays to know where the strings are anagrams or not.

#include <stdio.h>
#include <string.h>

int main()
{
char s1, s2;
int is_anagram = 1;

//taking input
printf("Enter the first string\n");
scanf("%s",s1);
printf("Enter the second string\n");
scanf("%s",s2);

//two arrays
//and initializing every element to 0
int n1 = {0};
int n2 = {0};

//checking for anagram
int i = 0;
//making the elements of array equal to
//the frequency of characters
while(s1[i] != '\0')
{
n1[s1[i] - 'a']++;
i++;
}
i = 0;
while(s2[i] != '\0')
{
n2[s2[i] - 'a']++;
i++;
}

//comparing elements
for(i = 0; i<26; i++)
{
if(n1[i] != n2[i])
{
is_anagram = 0;
break;
}
}

if(is_anagram)
printf("Yes anagram\n");
else
printf("No\n");

return 0;
}

int n1 = {0} – We are initializing the array and making all of its element 0.

while(s1[i] != '\0') – Iterating over the string ‘s1’.

n1[s1[i] - 'a']++ – Finding the index of every character. s1[i] - 'a' gives us the ASCII the difference of the characters ‘s[i]’ and ‘a’ (e.g., ‘b’-’a’ is 1, ‘c’-‘a’ is 2, etc.).

Then we are just comparing both the arrays in the last ‘for’ loop.

Checking whether a string is a palindrome or not

A string is said to be a palindrome if it reads same from both front and back. For example, ‘level’ is a palindrome. We can easily check for palindromes in C by just iterating from front and back and checking if they are same characters or not. So, let’s do it.

#include <stdio.h>
#include <string.h>

int main()
{
char s;
//taking input
printf("Enter the string\n");
scanf("%s",s);

int i, is_pali = 1;

//checking for palindrome
for(i=0; i < strlen(s)/2; i++)
{
if(s[i] != s[strlen(s)-i-1])
{
is_pali = 0;
break;
}
}

if(is_pali)
printf("Yes, palindrome\n");
else
printf("No, not palindrome\n");

return 0;
}

for(i=0; i < strlen(s)/2; i++) – We are iterating from the first element to the middle element of the string.

if(s[i] != s[strlen(s)-i-1]) – Comparing each character with the corresponding character from the end. For example, when ‘i’ is 0, s[i] is the first character and s[strlen(s) -i -1] (s[strlen(s)-1]) is the last character of the string. You can read more about strlen from this post.

Converting a string into uppercase and lowercase

We are going to use the ctype’s tolower and toupper functions for this task.

#include <stdio.h>
#include <ctype.h>

void strupr(char s[])
{
int i = 0;
while(s[i] != '\0')
{
printf("%c",toupper(s[i]));
i++;
}
printf("\n");
}

void strlwr(char s[])
{
int i = 0;
while(s[i] != '\0')
{
printf("%c",tolower(s[i]));
i++;
}
printf("\n");
}

int main()
{
strupr("Hello BlogsDope");
strlwr("Hello BlogsDope");
return 0;
}

Liked the post?
Developer and founder of CodesDope.
1 COMMENT