Character Array And Functions
Question:1
C program to input for a string, pass it to a function, and display the string.
Sol:
#include<stdio.h> void show(char n[]) { int i=0; while(n[i]!='\0') { printf("%c\n",n[i]); i++; } } int main() { char n[30]; printf("Enter any string "); gets(n); show(n); }
Output:
Enter any string Hello hi H e l l o h i
Question:2
C program to input for a string, pass it to a function and display the string and its length.
Sol:
#include<stdio.h> void show(char n[]) { int i=0; while(n[i]!='\0') { printf("%c\n",n[i]); i++; } printf("Length of string %d\n",i); } int main() { char n[30]; printf("Enter any string "); gets(n); show(n); }
Output:
Enter any string computer world c o m p u t e r w o r l d Length of string 14
Question:3
C program to input for a string, pass it to a function and display total alphabets in the string.
Sol:
#include<stdio.h> void show(char n[]) { int i=0,a=0; while(n[i]!='\0') { printf("%c\n",n[i]); if((n[i]>='A' && n[i]<='Z') || (n[i]>='a' && n[i]<='z')) a++; i++; } printf("Length of string %d\n",i); printf("Total alphabets in string %d\n",a); } int main() { char n[30]; printf("Enter any string "); gets(n); show(n); }
Output:
Enter any string hello234#$# h e l l o 2 3 4 # $ # Length of string 11 Total alphabets in string 5