C Programming : String Handling (BIM-TU)

By Suraj Chaudhary

This article presents a few examples of string handling-related problems. You will learn how to deal with strings, count words, print patterns, etc.

1. Write a program to input a word and count the number of vowels on it.

#include<stdio.h>
#include<string.h>
int main()
{
    char a[100];
    int count=0,i;
    printf("Enter a word:");
    scanf("%s",&a);
    for(i=0;i<strlen(a);i++)
    {
        if(a[i]=='a' || a[i]=='e' || a[i]=='i' || a[i]=='o' || a[i]=='u' ||a[i]=='A' || a[i]=='E' || a[i]=='I' || a[i]=='O' || a[i]=='U')
        count++;
    }
    printf("Number of vowels: %d",count);
    return 0;
}
Enter a word:highapproach
Number of vowels: 4

2. WAP to input 3 words and print the shortest word.

#include<stdio.h>
#include<string.h>
int main()
{
    char a[100],b[100],c[100];
    int x=0,y=0,z=0,i;
    printf("Enter any three words:");
    scanf("%s%s%s",&a,&b,&c);
    for(i=0;i<strlen(a);i++)
    {
        x++;
    }
    for(i=0;i<strlen(b);i++)
    {
        y++;
    }
    for(i=0;i<strlen(c);i++)
    {
        z++;
    }
    if(x<y && x<z)
    printf("%s is smallest",a);
    else if(y<x && y<z)
    printf("%s is smallest",b);
    else
    printf("%s is smallest",c);
    return 0;
}
Enter any three words:Ashesh Bishowraj Amitkumar
Ashesh is smallest

3. WAP to input a word and check whether it is palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char a[20],b[20];
    clrscr();
    printf("Enter a word:");
    scanf("%s",&a);
    strcpy(b,a);
    if(strcmpi(b,strrev(a))==0)
    printf("Palindrome");
    else
    printf("Not palindrome");
    getch();
}
Enter a word: Level
Palindrome

4. Write a program to print the following series:

N
NE
NEP
NEPA
NEPAL

#include<stdio.h>
int main()
{
    char a[]="NEPAL";
    int i,j;
    for(i=0;i<=4;i++)
    {
        for(j=0;j<=i;j++)
        {
            printf("%c",a[j]);
        }
        printf("\n");
    }
    return 0;
}

5. Write a program to print the following series:

image 3
#include<stdio.h>
int main()
{
    char a[]="NEPAL";
    int i,j,k,sp=2;
    for(i=0;i<=4;i=i+2)
    {
        for(k=0;k<sp;k++)
        {
            printf(" ");
        }
        for(j=0;j<=i;j++)
        {
            printf("%c",a[j]);
        }
        printf("\n");
        sp--;
    }
    return 0;
}

6. Write a program to print the following series:

image 5
#include<stdio.h>
int main()
{
    char a[]="KATHMANDU";
    int i,j,k,sp=0;
    for(i=8;i>=0;i=i-2)
    {
        for(k=1;k<=sp;k++)
        {
            printf(" ");
        }
        for(j=0;j<=i;j++)
        {
            printf("%c",a[j]);
        }
        printf("\n");
        sp++;
    }
    return 0;
}

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resource to living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

Leave a Comment

Slide to prove you're not a bot/spammer *