C Language: Pointers 12

Pointers Functions and Character Arrays

Question:1
C program to take input for a string , pass the string to a function and display the string in the given format?
example:
n=”hello”
h
e
l
l
o
Sol:

#include<stdio.h>
void show(char *p)
{
	while(*p!='\0')
	{
		printf("%c\n",*p);
		p++;
	}
  
}
int main()
{
  char n[20];
  printf("Enter any string ");
  gets(n);
  show(n);
  /* show(&n[0]); */
  return(0);
}
/* Output */
Enter any string computer
c
o
m
p
u
t
e
r

Question:2
C program to take input for a string, pass the string to a function and display the string in the given format and also print its length?
example:
n=”hello”
h
e
l
l
o
length=5
Sol:

#include<stdio.h>
void show(char *p)
{
	int i=0;
	while(*p!='\0')
	{
		printf("%c\n",*p);
		i++;
		p++;
	}
	printf("length = %d\n",i);
  
}
int main()
{
  char n[20];
  printf("Enter any string ");
  gets(n);
  show(n);
  /* show(&n[0]); */
  return(0);
}
/* Output */
Enter any string hello
h
e
l
l
o
length = 5

Question:3
C program to take input for a string, pass the string to a function and display the string and also display the following:
1. length of the string
2. total alphabets
3. total upper case alphabets
4. total lower case alphabets
5. total digits
6. total spaces
7. total special characters
Sol:

#include<stdio.h>
void show(char *p)
{
	int i=0,a=0,ua=0,la=0,d=0,sp=0,spl=0;
	while(*p!='\0')
	{
		printf("%c\n",*p);
		if((*p>='A' && *p<='Z') || (*p>='a' && *p<='z'))
		{
			a++;
			if(*p>='A' && *p<='Z')
			ua++;
			else
			la++;
		}
		else
		  if(*p>='0' && *p<='9')
		  d++;
		  else
		    if(*p==' ')
		    sp++;
		    else
		    spl++;
		i++;
		p++;
	}
	printf("length %d total alpha %d upper alpha %d lower alpha %d\n",i,a,ua,la);
	printf("digits %d space %d special chars %d\n",d,sp,spl);
  
}
int main()
{
  char n[20];
  printf("Enter any string ");
  gets(n);
  show(n);
  /* show(&n[0]); */
  return(0);
}
/* Output */
Enter any string hel34$% lo45
h
e
l
3
4
$
%

l
o
4
5
length 12 total alpha 5 upper alpha 0 lower alpha 5
digits 4 space 1 special chars 2