Pages

Saturday, August 1, 2020

C PROGRAM TO COUNT VOWELS IN A STRING

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int L, i, v=0;
puts("Enter any sentence: ");
gets(s);
L=strlen(s);

// Loop to find vowels in the given string
for(i=0; i<L; i++)
{
switch(s[i])
{
case 'a';   case'A';


case 'e':   case 'E':
case 'i':    case 'I':
case 'o':   case 'O':
case 'u':   case 'U':
v++;
}
}
printf("\nTotal vowels are : %d", v);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter any sentence:
Once upon a time

Total number of vowels: 7

C PROGRAM TO COUNT VOWELS AND CONSONANT IN A STRING


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int L, i, v=0, c=0;
puts("Enter any sentence: ");
gets(s);
L=strlen(s);

// Loop to count total number of vowels and consonants
for(i=0; i<L; i++)
{
if (s[i]=='a' || s[i]=='A' || s[i]=='e' || s[i]=='E' || s[i]=='i' || s[i]=='I' || s[i]=='o' || s[i]=='O' || s[i]=='u' || s[i]=='U')
{
v=v+1;
}

else if (s[i] == ' ')
{
}

else
{
c=c+1;
}
}

printf("\nTotal vowels are : %d", v);
printf("\nTotal consonants are : %d", c);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter any sentence:
Once upon a time

Total vowels are: 7
Total consonants are: 6

SEARCHING IN STRING ARRAY



Searching in string array is like searching in an integer array.
In the following program we are going to searching a given character position in the given sentence by user.

PROGRAM TO SEARCH IN STRING
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60], c;
int i, L;
puts("Enter any sentence: ");
gets(s);

printf("Enter character to search:");
c=getche();

// Calculating strength of the string 
L=strlen(s);

// Loop to search character position in the string
for(i=0; i<L; i++)
{
if(c == s[i])
{
break;
}
}

printf("\nCharacter position found at the position :%d", i+1);
getch();
}


----------------------------
         OUTPUT 
----------------------------

Enter any sentence:
Computer is an example of electronic device.


Enter character to search: s
Character found at position: 11

STRCMP FUNCTION IN C



The strcmp function is used to compare two string values.
It return 0, if both string values match exactly and return non zero value, if string values does not match.

STRCMP() FUNCTION EXAMPLE
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char a[60], b[60];
int c;
puts("Enter first string value: ");
gets(a);

puts("Enter second string value: ");
get(b);

c=strcmp(a, b);
if(c==0)
{
printf("String values are same");
}
else
printf("String values are different");

getch();
}


----------------------------
         OUTPUT 
----------------------------

Enter first string value:
Computer

Enter second string value:
Computer

String values are different

SEARCHING IN STRING ARRAY >>

STRCPY FUNCTION IN C



The strcpy function is used to copy one string variable's value in another string variable.

STRCPY() FUNCTION EXAMPLE
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char a[60], b[60];
puts("Enter any sentence: ");
gets(a);
strcpy(b, a);

// Print values of string variable A and B
printf("\n- - - Value of string variable A - - -\n");
printf("%s", a);


printf("\n- - - Value of string variable B - - -\n");
printf("\n%s", b);
getch();
}


----------------------------
         OUTPUT 
----------------------------

Enter any sentence:
Computer is an electronic device

Value of string variable A
Computer is an electronic device

Value of string variable B
Computer is an electronic device

STRCMP FUNCTION IN C >>

STRREV FUNCTION IN C



The strrev() function is used to reverse the selected string value.

STRREV() FUNCTION EXAMPLE
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
puts("Enter any sentence: ");
gets(s);
printf("The reversed string is : %s", strrev(s));
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter any sentence:
Computer
retupmoC

C PROGRAM TO REVERSE STRING WITHOUT STRREV() FUNCTION
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int i, L
puts("Enter any sentence: ");
gets(s);



// Loop to print string in reversed form.
for(i=L; i>=0;  i--)
{
printf("%c",s[i]);
}
getch();

}


----------------------------
         OUTPUT 
----------------------------

Enter any sentence:
Computer
retupmoC

STRCPY IN C >>

STRLEN FUNCTION IN C



The strlen function is used to calculate total length of the given string.
STRLEN() FUNCTION EXAMPLE
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int L;
puts("Enter any sentence: ");
gets(s);
L=strlen(s);
printf("Total length is : %d", L);
getch();
}

----------------------------
         OUTPUT 
----------------------------


Enter any sentence:
My name is rakesh

Total length is: 17

C PROGRAM TO CALCULATE STRING LENGTH WITHOUT STRLEN() FUNCTION
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char s[60];
int i, count=0;
puts("Enter any sentence: ");
gets(s);



// Loop to count total length of the given string
for(i=0; s[i] != '\0'; i++)
{
count++;
}


printf("Total length is : %d", count);

getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter any sentence:
My name is rakesh

Total length is: 17


Friday, July 31, 2020

GETS AND PUTS FUNCTION IN C

GETS FUNCTION
The gets() function is used to read only the string values in C programming language.

SYNTAX OF GETS() FUNCTION
gets(string variable name);

DIFFERENCE BETWEEN SCANF() AND GETS()
The scanf() function is used to read different datatypes inputs (character, integer, float, string) but it cannot read the blank spaces. 
The gets() function is used to read only the string values and it can also read the blank space between the string values. 

GETS() FUNCTION EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[40];
printf("Enter your name: ");
gets(name);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter your name:
Rakesh kumar


PUTS FUNCTION
The puts() function is used to display/print only the string values in C programming language.

SYNTAX OF GETS() FUNCTION
puts(string variable name);

DIFFERENCE BETWEEN PRINTF() AND PUTS()
The printf() function is used to display/print the different datatype variable's values (character, integer, float, string)
The puts() function is used to display/print only the string values.

We cannot combine the text message + string variable's value in the puts() function, to combine the text message + variable's value we need to use the printf() function.  

PUTS() FUNCTION EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char name[40];
printf("Enter your name: ");
gets(name);
puts(name);
getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter your name:
Rakesh kumar
Rakesh kumar

STRLEN IN C >>