String Solved Question
1. C program to count number of alphabets, digits and special characters in string.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
    char str[MAX_SIZE];
    int alphabets, digits, others, i;
    alphabets = digits = others = i = 0;
    /* Input string from user */
    printf("Enter any string : ");
    gets(str);
    while(str[i]!='\0')
    {
        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
        {
            alphabets++;
        }
        else if(str[i]>='0' && str[i]<='9')
        {
            digits++;
        }
        else
        {
            others++;
        }
        i++;
    }
    printf("Alphabets = %d\n", alphabets);
    printf("Digits = %d\n", digits);
    printf("Special characters = %d", others);
    return 0;
}
2.C program to count total number of vowels and consonants in a string.
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
    char str[MAX_SIZE];
    int i, len, vowel, consonant;
    /* Input string from user */
    printf("Enter any string: ");
    gets(str);
    vowel = 0;
    consonant = 0;
    len = strlen(str);
    for(i=0; i<len; i++)
    {
        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
        {
            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'  )
                vowel++;
            else
                consonant++;
        }
    }
    printf("Total number of vowel = %d\n", vowel);
    printf("Total number of consonant = %d\n", consonant);
    return 0;
}
3. C program to count number of words in a string.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
    char str[MAX_SIZE];
    int i, words;
    /* Input string from user */
    printf("Enter any string: ");
    gets(str);
    i = 0;
    words = 1;
    while(str[i] != '\0')
    {
        /* If the current character(str[i]) is white space */
        if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
        {
            words++;
        }
        i++;
    }
    printf("Total number of words = %d", words);
    return 0;
}
4. C Program to find reverse of a string without using String Handling Function.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
    char str[MAX_SIZE], reverse[MAX_SIZE];
    int i, strIndex, revIndex, len;
    /* Input string from user */
    printf("Enter any string: ");
    gets(str);
    /* Find length of string */
    i = 0;
    while(str[i] != '\0') i++;
    len = i;
    /* 
     * Store each character from end of original string 
     * to reverse string
     */
    revIndex = 0;
    strIndex = len - 1;
    while(strIndex >= 0)
    {
        reverse[revIndex] = str[strIndex];
        strIndex--;
        revIndex++;
    }
    reverse[revIndex] = '\0';
    printf("\nOriginal string = %s\n", str);
    printf("Reverse string = %s", reverse);
    return 0;
}
5. C program to check whether a string is palindrome or not without using string handling function.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
    char str[MAX_SIZE];
    int len, startIndex, endIndex;
    /* Input string from user */
    printf("Enter any string: ");
    gets(str);
    /* Find length of the string */
    len = 0;
    while(str[len] != '\0') len++;
    startIndex = 0;
    endIndex   = len-1;
    
    while(startIndex <= endIndex)
    {
        if(str[startIndex] != str[endIndex])
            break;
        startIndex++;
        endIndex--;
    }
    if(startIndex >= endIndex)
    {
        printf("String is Palindrome.");
    }
    else
    {
        printf("String is Not Palindrome.");
    }
    return 0;
}
6. C program to sort set(number) of string given by user in ascending (alphabetical) order.
#include<stdio.h>
#include<string.h>
int main(){
   int i,j,count;
   char str[25][25],temp[25];
   puts("How many strings u are going to enter?: ");
   scanf("%d",&count);
   puts("Enter Strings one by one: ");
   for(i=0;i<=count;i++)
      gets(str[i]);
   for(i=0;i<=count;i++)
      for(j=i+1;j<=count;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
   printf("Order of Sorted Strings:");
   for(i=0;i<=count;i++)
      puts(str[i]);
   
   return 0;
}
7. C program to remove spaces, blanks from a string.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // Maximum string size
/* Function declaration */
char * removeBlanks(const char * str);
int main()
{
    char str[MAX_SIZE];
    char * newString;
    printf("Enter any string: ");
    gets(str);
    printf("\nString before removing blanks: \n'%s'", str);
    newString = removeBlanks(str);
    printf("\n\nString after removing blanks: \n'%s'", newString);
    return 0;
}
/**
 * Removes extra blank spaces from the given string
 * and returns a new string with single blank spaces
 */
char * removeBlanks(const char * str)
{
    int i, j;
    char * newString;
    newString = (char *)malloc(MAX_SIZE);
    i = 0;
    j = 0;
    while(str[i] != '\0')
    {
        /* If blank space is found */
        if(str[i] == ' ')
        {
            newString[j] = ' ';
            j++;
            /* Skip all consecutive spaces */
            while(str[i] == ' ')
                i++;
        }
        newString[j] = str[i];
        i++;
        j++;
    }
    // NULL terminate the new string
    newString[j] = '\0';
    return newString;
}
8. Print the pattern
a)
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER
#include<stdio.h>
int main()
{
  // variable
  char str[20];
  // take input
  printf("Enter a string: ");
  scanf("%[^\n]",str);
  // print pattern
  // outer loop for row
  for(int i=0; str[i]!='\0'; i++)
  {
    // inner loop for column
    for(int j=0; j<=i; j++)
    {
      // display
      printf("%c", str[j]); 
    }
    printf("\n"); // new line
  }
  return 0;
}
b)
H
HE
HEL
HELL
HELLO
HELL
HEL
HE
H
#include<stdio.h>
#include<stdlib.h>
int main()
{
   // variables
   char str[20];
   int len, place;
   // take input
   printf("Enter a string: ");
   scanf("%[^\n]", str);
   // find length of string
   for(len=0; str[len]!='\0'; len++);
   // actual length is length - 1
   len--;
 
   // outer loop for row
   for(int i=0; i<(2*len+1); i++)
   {
     // find the place
     if(i<len) place = i;
     else place = abs(2*len - i);
     // inner loop for column
     for(int j=0; j<=place; j++)
     printf("%c",str[j]); // print
     printf("\n"); // next line
   }
   return 0;
}
c)
#include<stdio.h>
int main()
{
  // variable
  char str[20];
  int len;
  // take input
  printf("Enter a string: ");
  scanf("%[^\n]",str);
  // count number of characters
  for (len = 0; str[len]!='\0'; len++);
  // for first half
  for(int i=0; i<len; i++)
  {
    for(int j=0; j<len-i-1; j++)
    {
      printf(" "); // space
    }
    for(int j=0; j<=i; j++)
    {
      printf("%c", str[j]); // character
    }
    printf("\n"); // new line
  }
  // for second half
  for(int i=1; i<len; i++)
  {
    for(int j=0; j<i; j++)
    {
      printf(" "); // space
    }
    for(int j=i; j<len; j++)
    {
      printf("%c", str[j]); 
    }
    printf("\n"); // new line
  }
  
  return 0;
}
d)
#include<stdio.h>
int main()
{
   // variables
   char str[20];
   int n, a=0;
   // take input values
   printf("Enter a string: ");
   scanf("%[^\n]", str);
   printf("Enter number of rows: ");
   scanf("%d", &n);
   // outer loop for row
   for(int i=0;i<=n;i++)
   {
     // for space 
     for(int j=0;j<=n-i;j++) 
     printf(" "); // print space
     for(int k=0;k<=i;k++)
     {
        // print character
        printf("%2c", str[a++]);
        // if index reach end of string then again
        // it should start from initial characters
        if(str[a]=='\0') a=0;
     }
     printf("\n"); // new line
   }
   return 0;
}