String Coding Questions with solutions.

String coding questions

This section will cover all the questions that might be asked in any of your interviews or coding round. The string programs are written in basic C Language. In this section theory is not covered, this section focuses to improve practical coding knowledge of strings.

After reading this blog you will be able to answer few questions :

  • How do you approach a string problem?
  • How String is useful and used?
  • What are the basic coding questions?
  • String interview Questions.

The string coding questions is the most famous topic that is asked in the interviews or in the technical round, please check out some important questions from below :

Top 21 String programming questions with solutions.

  1. Write a program in C to input a string and print it. Test Data > Input the string : Hello Output : String is Hello
1)scanf("%c", &input);  //taking character (single letter) as input
2)scanf("%s", input) ; // taking single word as input without spaces
3)scanf("%[^\n]s" ,input); // taking sentence as input having spaces
        or
 fgets(input ,sizeof input,stdin);

//Sample program
#include <stdio.h>
void main()
{
    char a[10];
    printf("Enter string");
    scanf("%s",a);
    printf("String is %s",a);
}

2. How to find the length of the string?

//1st method using strlen function

#include <stdio.h>
#include <string.h>
void main()
{
 char str[100]= "Hello";
 int len =strlen(str);
 printf("Length is %d ",len);
}

//2nd method without using strlen function

#include<stdio.h>
void main()
{
     char str[100] = "Hello";
     int i,count =0;
     for(i=0;str[i]!='\0';i++)
     {
      count ++;
     }
     printf("Length is %d",count);
}

3. How to separate the individual characters from a string?

#include <stdio.h>
#include <string.h>
int main()
{
    int i;
   char word[50];
   int len =0;
   printf("press ");
   scanf("%s",word);
   for(i=0;word[i]!='\0';i++)
   {
       len ++;
   }
   
  
  // int len2=strlen(word);
   // printf("Length is %d",len2);
    for(i=0;i<len;i++)
    {
        
        printf("%c ",word[i]);
    }
}

4. Write a program to reverse a string

#include <stdio.h>
#include <string.h>
int main()
{
    char str[10];
    int i,n;
    int count =0;
    printf("Enter string");
    scanf("%s",str);
    n= strlen(str);
    for(i=n;i>=0;i--)
    {
       printf("%c ",str[i]);
    }

5. Write a program to count the number of words

#include <stdio.h>
#include <string.h>
int main()
{
    int i,j;
   char word[50];
   char str[50]="hola my name";
   int len1=1;
   int count=1;
   for(i=0;str[i]!='\0';i++)
   {
       if(str[i]==' '||str[i]== '\t'||str[i]== '\n')
       {
          count++; 
       }
   }
    printf("no. of words %d",count); 
}

6. Write a program to compare strings

#include <stdio.h>
#include <string.h>
int main ()
{
  //char str[10];
  char str1[100]="my name";
  char str2[100]="my name"; 
  int i, n;
  int count = 1;
   n= strcmp(str1,str2);
  if(n==0)
    printf("equal");
    else
    printf("not equal");
}

7. Write a program to count alphabets and numbers

#include <stdio.h>
#include <string.h>
int main ()
{
  //char str[10];
  char str1[100]="my12An&ame";
  char str2[100]="my name "; 
  int i, n;
  int count = 0;
  int count2 = 0;
  int count3 = 0;
  for(int i=0;str1[i]!='\0';i++)
  {
    if(str1[i]>='a' && str1[i] <='z' || str1[i]>='A' && str1[i] <='Z')
   {
     count ++;
   }
   else if (str1[i]>='0' && str1[i]<='9')
   {
       count2++;
   }
   else
   {
       count3 ++;
   }
  }
  printf("Alphabets is %d",count);
  printf("Numbers is %d",count2);

8. How to find vowels and consonants from a string ?

#include <stdio.h>
#include <string.h>
int main ()
{
  //char str[10];
  char str1[100]="aeibc";
  char str2[100]="my nam"; 
  int i, n;
  int count = 0;
  int count2 = 0;
  int count3 = 0;
  for(int i=0;str1[i]!='\0';i++)
  {
   if(str1[i] == 'a'||str1[i] =='e'||str1[i] =='i'||str1[i] =='o'||str1[i] =='u'||str1[i] =='A'||str1[i] =='E'||
   str1[i] =='I'||str1[i] =='O'||str1[i] =='U')
   {
       count3 ++;
   }
   else
   {
       count ++;
   }
  }
  printf("consonants is %d",count);
  printf("Vowels is %d",count3);
  
}

9. How to concat two strings in C language?

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

void main ()
{
  char str1[100]="Techno";
  char str2[100]="name"; 
  char str[100];
  strcat(str1,str2);
  printf("%s",str1);
}

10. How to count the maximum occurrence of a character in string?

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[100];
  char result;
  char freq[256] ={0};
  printf("Enter string");
  scanf("%s",str);
  int i, max;
  int count = 0;
  max=0;
  
  for(int i=0;str[i]!='\0';i++)
  {
     freq[str[i]]++;
  }
  for(int i=0;str[i]!='\0';i++)
  {
     if(freq[str[i]]>max)
     {
         max =freq[str[i]];
         result =str[i];
     }
  }
  printf("Maximmum frequency character is %c %d times",result,max);
  
}

//max frequency of integer array
#include <stdio.h>
void main()
{
  int n,i,temp,r,sum=0,a[100],max=0,ans;
  int freq[100]={0};
  printf("enter");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      scanf("%d",&a[i]);
      freq[a[i]]++;
  }
  
 for(i=0;i<n;i++)
 {
     if(freq[a[i]]>max)
     {
         max=freq[a[i]];
         ans =a[i];
     }
 }
 printf("max is %d and %d times",ans,max);
 
}

11 . How to sort a string?

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[100];
  char ch;
  int i,j;
  printf("Enter string");
  scanf("%s",str);
  int l = strlen(str);
  for( i=0; i<l;i++)
  {
      for(j=i+1;j<l;j++)
      {
          if(str[i]>str[j])
          {
              ch =str[i];
              str[i]=str[j];
              str[j]=ch;
          }
      }
  }
  
  printf("Sorted string is %s",str);

12. How to convert string from uppercase to lowercase and vice-versa?

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


int main()
{
    char a[100];
    int ch;
    printf("enter string");
    fgets(a,sizeof 
    a, stdin);
    int i,j;
    for(i=0; a[i]!='\0';i++)
    {
        ch =islower(a[i]) ? toupper(a[i]) : tolower(a[i]);
        putchar(ch);
        
    }
}

II method : 

  for(i=0;i<len;i++)
    {
    ch[i] =islower(a[i]) ? toupper(a[i]) :tolower(a[i]);
     
    }
    for(i=0;i<len;i++)
    {
        printf("%c ",ch[i]);
    }

13. How to find a particular sub string from string ?

int main()
{
    char a[100];
    int ch,t,h,e,spc,freq=0;
    printf("enter string");
    fgets(a,sizeof 
    a, stdin);
    int i,j;
    for(i=0; a[i]!='\0';i++)
    {
      t= (a[i]=='t' ||a[i]=='T');
      h=(a[i+1]=='h' || a[i+1]=='h');
      e=(a[i+2]=='e' || a[i+2]=='e');
      spc=(a[i+3]==' ' || a[i+3]==' ');
      if(t&&h&&e&&spc ==1)
      {
          freq++;
      }
    }
    printf("%d",freq);
}

14. How to remove numbers from the string?

int main()
{
    char str[100],str2[100];
    int ch,t,h,e,spc,count=0,j;
    printf("Enter string");
    fgets(str, sizeof str , stdin);
    int i;
    for(i=0,j=0;str[i]!='\0';i++)
    {
        if((str[i]>='a' && str[i]<='z') ||(str[i]>='A' && str[i]<='Z'))
        {
            str2[j]=str[i];
            j++;
        }
    }
    str2[j]='\0';
    printf("string is %s",str2);
}

II method :

#include<stdio.h>
void main()
{
    char a[100];
    int i,len=0;
    printf("enter string");
    scanf("%[^\n]s",a);
    for(i=0;a[i]!='\0';i++)
    {
        len ++;
    }
    for(i=0;i<len;i++)
    {
        if(a[i]>='0' &&a[i]<='9')
        {
            a[i]='\0';
        }
    }
    for(i=0;i<len;i++)
    {
        printf("%c",a[i]);
    }
}

15. How to find frequency of particular character in a string ?

int main()
{
    char str[100],str2[100],c;
    int t=0,count=0,j;
    printf("Enter string");
    fgets(str, sizeof str , stdin);
    int i;
    printf("Enter character");
    scanf("%c",&c);
    for(i=0;str[i]!='\0';i++)
    {
       
       t= (str[i]==c);
       if(t==1)
       {
           count++;
       }
    }
    printf("count is %d",count);
}

II method :

#include<stdio.h>
void main()
{
    char a[100];
    int i,len=0;
    printf("enter string");
    scanf("%[^\n]s",a);
    for(i=0;a[i]!='\0';i++)
    {
        len ++;
    }
    for(i=0;i<len;i++)
    {
        if(a[i]=='5' &&a[i+1]=='6')
        {
            a[i]='\0';
            a[i+1]='\0';
        }
    }
    for(i=0;i<len;i++)
    {
        printf("%c",a[i]);
    }
}

16. How to check uppercase and lowercase characters in string?

int main()
{
   
    char str[100],ch;
    int i,max,t;
    printf("Enter char");
    scanf("%c",&ch);
    if(isupper(ch))
    {
        printf("Uppercase");
    }
    else
    printf("Lowercase");
    
Method 2:
#include<stdio.h>
#include<ctype.h>
void main()
{
    char a[100],b[100];
    int i,n=0;
    printf("enter str");
    scanf("%[^\n]s",a);
    for(i=0;a[i]!='\0';i++)
    {
        n++;
    }
    for(i=0;i<n;i++)
    {
        b[i]=(islower(a[i]) ?  toupper(a[i]): tolower(a[i]));
    }
    for(i=0;i<n;i++)
    {
        printf("%c ",b[i]);
    }
}
}

17 . How to check whether the character is hexadecimal or not?

int main()
{
   
    char str[100],ch;
    int i,max,t;
    printf("Enter char");
    scanf("%c",&ch);
    
       if(isxdigit(ch))
    {
        printf("hexadecimal");
    }
    else
    printf("not");
    //Second method 
    if((ch >='0' &&ch<='9') ||(ch >='a' &&ch<='f')||(ch >='A' &&ch<='F'))
    {
         printf("hexadecimal");
    }
    else
    {
    printf("not");
    }
  
    
}

18. How to replace string with some special character?

int main()
{
    char str[100];
    char ch='*';
    fgets(str,sizeof str,stdin);
    int i;
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]==' ')
        {
            str[i]=ch;
            
        }
    }
    printf("String is %s",str);
}

19. How to replace a particular string?

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

int main()
{
    char str[100];
    char ch[100]="is";
    int count=0;
    
    fgets(str,sizeof str,stdin);
    int i;
    for(i=0;str[i]!='\0';i++)
    {
      
        if(str[i]=='t' && str[i+1]=='h' &&str[i+2]=='e')
        {
           
           str[i]='m';
           str[i+1]='m';
           str[i+2]='m';
            
        }
    }
    printf("str is %s",str);
}

20 . How to check string entered is digit or not?

#include <stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char str[100]; 
    char temp;
   printf("Enter char");
   scanf("%c",&temp);
   
   if(isdigit(temp))
   {
       printf("digit");
   }
   else
   {
     printf("not");
   }
}

21. How to convert vowels into upper case characters in a given string?

This question is solved in two ways, if you know another way then please share, your solution will be published here.

int main()
{
    char str[255],ch; 
    char temp;
   printf("Enter string");
   fgets(str, sizeof str,stdin);
   int max =0,i;
   int n=strlen(str);
   for(i=0;i<n;i++)
   {
       if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
       {
           str[i]=str[i]-32;    // or str[i]=str[i]-'a'+'A';
        
       }
   }
   printf("String is %s",str);
}

II method :

#include<stdio.h>
#include<ctype.h>
void main()
{
    char a[100],b[100];
    int i,n=0;
    printf("enter str");
    scanf("%[^\n]s",a);
    for(i=0;a[i]!='\0';i++)
    {
        n++;
    }
    for(i=0;i<n;i++)
    {
       if(a[i]=='a' || a[i+1]=='e' || a[i+1]=='i'|| a[i+1]=='o' || a[i+1]=='u')
       {
           b[i]= toupper(a[i]);
       }
       else
       {
           b[i]=a[i];
       }
    }
     for(i=0;i<n;i++)
    {
      printf("%c",b[i]);
    }
    
}

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

In case you know some other important questions then you can leave a comment here or mail us at technonamecontact@gmail.com, So anyone can a get chance to publish their code on our website.

As a result, one can improve his knowledge and spread with others

Please check TCS Digital Coding Questions and solutions from here which were previously asked.

For understanding the most important concept data structures in an easy way you can check this article on our website.


5 responses to “String Coding Questions with solutions.”

  1. John says:

    Please post more questions

  2. George Gomes says:

    I found that this will really give me a good understanding of String.

  3. […] further expand your understanding of programming concepts, check out our article on the most frequently asked questions about strings, complete with examples. This will give you a deeper understanding of string operations and […]

  4. […] further improve your understanding of programming concepts please check out our article on the most frequently asked questions about strings, complete with […]

Leave a Reply

Your email address will not be published. Required fields are marked *