To find no. of alphabets, digits & white spaces in a string entered by user.

#include<stdio.h>
#include<string.h>
main()
{
int a,b,c=1,d=0,e=0,f=0,g=1;
char s1[100],s2[100];
printf("Enter a string\n");
gets(s1);

b=strlen(s1);
for(a=1;a<=b-1;a++)
{
if(isalpha(s1[a]))
{
c++;
}

if(isspace(s1[a]))
{
e++;
}
if(isdigit(s1[a]))
{
f++;
}
}
printf("Digits = %d, Alphabets = %d, Space = %d",f,c,e);

}

 

Output:

 

Enter a string

hi 55

Digits = 2, Alphabets = 2 & Space = 1

 

 

String Operations in one programs – Strcpy, Strcmp, Strcat, Strupr, Strlwr, Strlen, Strrev

#include<stdio.h>
#include<string.h>
main()
{
    int a,b;
    char s1[100],s2[100];
    printf("Enter a string.\n");
    scanf("%s",s1);
    //strlen(),strrev(),strcat(),strcpy(),strupr(),strlwr(),strcmp()
    printf("Enter the operation you wish to perform on the string:\n1. String Length\n2. String Reverse\n3. String Concatenation\n4. String Copy\n5. String Upper Case\n6. String Lower Case\n7. String Comparison\n");
    scanf("%d",&a);

    switch(a)
    {
    case 1:
        {
            b=strlen(s1);
            printf("String Length = %d",b);
            break;
        }
    case 2:
        {
            printf("String Reverse = %s",strrev(s1));
            break;
        }
    case 3:
        {
            printf("Enter the target string.\n");
            scanf("%s",s2);
            strcat(s2,s1);
            printf("Result - %s",s2);
            break;
        }
    case 4:
        {
            strcpy(s2,s1);
            printf("Copied String - %s",s2);
            break;
        }
    case 5:
        {
            printf("The Upper Case of String is %s",strupr(s1));
            break;
        }
    case 6:
        {
            printf("The Lower Case of String is %s",strlwr(s1));
            break;
        }
    case 7:
        {
            printf("Enter the string you wish to compare with the previous string.\n");
            scanf("%s",s2);
            a=strcmp(s1,s2);
            printf("String Compare %d",a);
            break;
        }

    default:
        {
            printf("You have not entered a valid option.");
        }
    }

}

 

 

Output:

Enter  a string.

hi

Enter the operation you wish to perform on the string:

1. String Length

2. String Reverse

3. String Concatination

4. String Copy

5. String Upper Case

6. String Lower Case

7. String Comparison

1

String Length = 2