Some common string problems with solutions in C
Strings
A string is a type of data type Used in most of the programming languages it can store characters as well as integers. So basically It stores ASCII value of a given character or an integer in the memory. So today I will share some courts I've written just some basic ones Because I'm learning to programme right now in C to be precise I will be sharing some questions as well as answers to those questions now these answers are completely made by me so if you are looking for the optimised solution this blog is not for you I'm pretty sure there are some more optimised neat and clean codes than mine, But I am a beginner and I know there are other people that are starting with programming so this blog might be helpful for students like me. If you want to suggest any corrections or modifications in these codes that are highly appreciable. Without any further due that my first share that how a string works this includes how it is stored in memory etc. And please note that all the assumption, information and codes are discussed with respect to C language
How a string works
- So basically string is a continuous array that can store integers, characters etc. Strings are declared by using char character data type.
char str[100];
As you can see about this is the way to declare strings in C, now the str[100] Just means it can store 99 characters or integers but you may wonder now why it can store hundred because the last memory location in a string goes to null character, which is '/0' it defines the termination of the end of string if you and 50 characters or integers in this string that can store 99 characters all other memory locations will be filled with null character. Space is also considered as a character so keep that in mind.
The ASCII value of characters Is stored in a string so if you store the letter A in the string, Apparently it's storing the ASCII value of A that is 65. Now if you are curious to know the ASCII values of other characters click here.
Now let's start with some questions about strings.
Question 1
Write a program to count total number of characters integers and special characters. In order to keep it simple special characters are everything other than characters and integers that includes (/,#,$,% etc.). No characters are basically A-Z and a-z, and Integers are simply (0,1,2,3,............)
Input | Output |
---|---|
Hello123# | Character- 5, Integer-3, Special Character- 1 |
ilove$teem!t | Character- 10, Integer-0, Special Character- 2 |
/Here's the answer :)
#include <stdio.h>
int main()
{
char str[40]; //Declaration sf string
scanf("%s",str); // User enters string
int i,alpha=0,chr=0,num=0;
for(i=0;str[i]!='\0';i++) // Using Loop to determine Character, integer or speacial character.
{
if(str[i]>='a'&&str[i]<='z' || str[i]>='A'&&str[i]<='Z')
{
alpha++;
}
else if(str[i]>='0' && str[i]<='9')
{
num++;
}
else
{
chr++;
}
}
printf("Characters-%d\nIntegers%d\nSpeacial Characters%d",alpha,num,chr); // Printing the output
return 0;
}
Question 2
Write a program to count the number of times vowels and consonants appear in a string, vowels are (a,e,i,o,u)
Input | Output |
---|---|
Hello | vowel - 2, Consonant - 3 |
i love steemit | vowel -6, Consonant - 6 |
#include <stdio.h>
int main()
{
char str[40]; //Declaration of string
scanf("%s",str); // User enters string
int i,vovel=0,conso=0;
for(i=0;str[i]!='\0';i++) // for loop to count vowels and consonants
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U' )
{
vovel++;
}
else
{
conso++;
}
}
printf("Vovels = %d\nConsonats = %d",vovel,conso);
return 0;
}
Question 3
Reverse given string.
Input | Output |
---|---|
Hello | olleH |
i love steemit | timeets evol i |
#include <stdio.h>
int main()
{
char str[100],temp; //Declaring string
gets(str); // Users enters string
int i,j,a;
a=strlen(str); //strlen gives the length of string
j=a;
for(i=0;i<a/2;i++) //For loop to swap string characters
{
temp=str[i];
str[i]=str[j-1];
str[j-1]=temp;
j--;
}
printf("%s",str); //printing output
return 0;
}
Question 4
**Check whether a string is palindrome or not. A palindrome is basically a sequence that is read same as for world as backwards **
Input | Output |
---|---|
Hello | Not a Palindrome |
wow | It is a Palindrome |
#include <stdio.h>
int main()
{
char str[100]; // Decleration of string
gets(str); // User enters string
int i,j,a,flag=0;
a=strlen(str); // Length of string
j=a;
for(i=0;i<a/2;i++) // For loop to check weather string is Palindrome or not
{
if(str[i]==str[j-1])
{
flag=0;
j--;
}
else
{
flag=1;
break;
}
}
if(flag==1)
{
printf("Not a Palindrome");
}
else
{
printf("It is a Palindrome");
}
return 0;
}
Question 5
Frequency of each Character in a string. You have to find frequency of each character,
Input | Output |
---|---|
Hello | H-1, e-1, l-2, o-1 |
wow | w-2, o-1 |
#include <stdio.h>
int main()
{
char str[100];
int m[100],i,j,n=0;
gets(str);
while(str[n]!='\0')
{
n++;
m[n]=1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(str[i]==str[j]&&i!=j&&m[i]!=0)
{
m[i]=m[i]+1;
m[j]=0;
}
}
}
for(i=0;i<n;i++)
{
if(m[i]!=0)
{
printf("%c - %d\n",str[i],m[i]);
}
}
return 0;
}
Question 6
Though the lexographic sorting of a array of strings, so basically lexography, Is a mathematical term used for dictionary order or alphabetical order, You have to Sort a array of string according to alphabetical order
Input | Output |
---|---|
5 bc ba ab dr m | ab ba bc dr m |
#include <stdio.h>
int main()
{
int m[100],i,j,n; // Declerations
scanf("%d",&n);
char str[n][50],temp[50];
for(i=0;i<n;i++)
{
scanf("%s",str[i]); //User enters strings
}
for(i=0;i<n-1;i++) // Loop to sort string based of alphabatical order
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]); //strcmp is a library function that returns the ascii diffrence between two strings
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
for(i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
return 0;
}
That's it for now this might not be the best presentation but I tried my best I hope you really enjoyed this blog was probably haven't read that but anyways thanks for stopping by have a nice day see you later. :)