Computer Science, asked by shreyasraghukumarp, 2 months ago

write a program to input a string and print the number of alphabets, numbers, spaces and special characters.
it should be java program without the use array.

Answers

Answered by Gupta7777777u
0
  1. #include <string.h>
  2. void stringcount(char *s)
  3. {
  4. static int i,alphabets=0,digits=0,specialcharacters=0;
  5. if(!s[i])
  6.    {
  7.     printf("Alphabets = %d\n",alphabets);
  8.        printf("Digits = %d\n",digits);
  9.        printf("Special characters = %d", specialcharacters);
  10.        return;
  11.    }
  12.    else
  13.    {
  14.    
  15.     if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) )
  16.          alphabets++;
  17.        else if(s[i]>=48 && s[i]<=57)
  18.         digits++;
  19.        else
  20.         specialcharacters++;
  21.        i++;
  22.         stringcount(s);
  23.    }  
  24. }
  25. int main()
  26. {
  27.    char s[1000];  
  28.  
  29.    printf("Enter  the string: ");
  30.    gets(s);
  31.    
  32.    stringcount(s);
  33. }

   

   

SOURCE> JAVA TUTORING

Answered by anitadevi052704
1

Answer:

program to find the total number of alphabets, digits or special characters in a string – In this article, we will brief in on the multiple methods to find the total number of alphabets, digits or special characters is a string in C programming.

C Program Check A Character Is Upper Case Or Lower Case

C Program To Count Total Number Of Notes in Given Amount

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

The methods used in this piece are as follows:

Using Standard Method

Using Function

Using Recursion

Using Pointers and While Loop

A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.

C Program Number Of Alphabets, Digits & Special Character In String

Firstly, you need to enter a particular string of your own choice, as per the given example above.

The string that is entered here is as follows:

“hello1234!@#$%”

As per the title of the blog, we need to find out the number of alphabets, digits and special characters given in the string.

Upon observation, it is seen that:

Alphabets: 4

Digits: 4

Special Characters: 5

Thus, the means used to find out so in C programming are as follows:

Using Standard Method

The ASCII values range of A to Z is 65 to 90, and a to z is 97 to 122 and 0 t0 9 is 48 to 57.

2) Read the entered string and initialize to s.

3) Iterate through string using for loop with the structure for(i=0;s[i];i++)

If the ASCII value of s[i] is in the range between 65 to 90 or 97 to 122 then increase the alphabets count.

If the ASCII value of s[i] is in the range between 48 to 57 then increase the digits count.

If the ASCII value of s[i] is not in the range of the above two conditions then increase the special characters count.

4) Print the alphabets count, digits count and special characters count.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

#include <stdio.h>

#include <string.h>

int main()

{

char s[1000];

int i,alphabets=0,digits=0,specialcharacters=0;

printf("Enter the string : ");

gets(s);

for(i=0;s[i];i++)

{

if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) )

alphabets++;

else if(s[i]>=48 && s[i]<=57)

digits++;

else

specialcharacters++;

}

printf("Alphabets = %d\n",alphabets);

printf("Digits = %d\n",digits);

printf("Special characters = %d", specialcharacters);

return 0;

}

Output:

1

2

3

4

Enter the string : hello1234!@#$%

Alphabets = 5

Digits = 4

Special characters = 5

Using Function

The main() calls the stringcount() function by passing the character array(string) as an argument.

2) The function stringcount() counts the number of alphabets, digits and special characters present in the given string as follows

C Program Check A Character Is Upper Case Or Lower Case

C Program To Count Total Number Of Notes in Given Amount

for loop iterates through the string until s[i] becomes null.

a) Increase the alphabet count if the ASCII value of s[i] is in the range between 65 to 90 or 97 to 122.

b) Increase the digits count if the ASCII value of s[i] is in the range between 48 to 57.

c) If s[i] is not in range of the above two conditions then increase the special characters count.

3) After all iterations of for loop the function prints the values of alphabets count, digits count and special characters count.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#include <stdio.h>

#include <string.h>

void stringcount(char *s)

{

int i,alphabets=0,digits=0,specialcharacters=0;

for(i=0;s[i];i++)

{

if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122) )

alphabets++;

else if(s[i]>=48 && s[i]<=57)

digits++;

else

specialcharacters++;

}

printf("Alphabets = %d\n",alphabets);

printf("Digits = %d\n",digits);

printf("Special characters = %d", specialcharacters);

}

int main()

{

char s[1000];

printf("Enter the string: ");

gets(s);

stringcount(s);

}

Output:

1

2

3

4

Enter the string: qwerty12345!@#$%

Alphabets = 6

Digits = 5

Special characters = 5

Using Recursion

The main() calls the stringcount() function, passing the character array(string) as an argument.

2) The function stringcount()

a) if there is no character is available in s[i] i.e s[i] is null then it prints the alphabets count, digits count and special characters count values.

b) If the string contains any characters then,

b.1) If the ASCII value of s[i] is in the range between 65 to 90 or 97 to 122 then increase the alphabets count by 1.

b.2) If the ASCII value of s[i] is in the range between 48 to 57 then digits count will be increased by 1.

b.3) If the ASCII value of s[i] is not in the range of alphabets and digits then increase the special characters count.

b.4) Increase the i value by 1, the function calls itself recursively until s[i] becomes null.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

#include <stdio.h>

#include <string.h

Explanation:

have a great day:)

Similar questions