Monday, September 30, 2024

Strings in C Language

C Stings

program to print whole string. 

main()

{

char str[50];

clrscr();

printf(“Enter the string= ”);

gets(str); //(print the space also)

//scanf(str);         //(print only one string)

printf(“\n the string is = %s”, str);

getch();

}

//getch(); //(not show the output)

// alt+f5 to show the output.


program to print whole string with fflush function.

#include<stdio.h>

#include<string.h>

main()

{

char str[50];

int age;

clrscr();

printf(“\n enter the age= “);

scanf(“%d”, age);

printf(“\n enter the string= “);

fflush(stdin); //this function removes the garbage

gets(str);

printf(“\n the string is = %s”, str);

printf(“\n the age is = %d”, age);

getch();

}

program for print whole string character by character.

#include<stdio.h>

#include<string.h>

main()

{

char str[50];

int i;

clrscr();

printf(“\n Enter the string = ” );

flush(stdin); //remove the garbage

gets(str);

printf(“\n The String is= “);

for(i=0;i<49;i++)

{

Printf(“%c”,str[i]); //print character by character

 }

getch();

}

//Note : loop ke sath %c hi lagega.


program for print small to capital letter character by character.

#include<stdio.h>

#include<string.h>

main()

{

char str[50];

int i,l;

clrscr();

printf(“\n enter the string = ” );

flush(stdin); //remove the garbage

gets(str);

l=strlen(str); //count string length

printf(“\n the string is= “);

for(i=0;i<l;i++)

{

printf(“%c”,str[i]);         //print character by character

printf(“%c”,str[i]-32); //print in capital letters.

 }

getch();

}

//note : loop ke sath %c hi lagega.


program for print integer into character from 65 to 121.

//means a to z and a to z.
#include<stdio.h>
#include<string.h>
main()
{
int i;
clrscr();
for(i=65;i<122;i++)
{
printf(“# ** %c = %d ** #”, i, i);
}
getch();
}
//note : for show A to Z and also a to z. 

program for print small to capital  or capital to small.

#include<stdio.h>
#include<string.h>
main()
{
char str[50];
int i,l;
clrscr();
printf(“\n enter the string = ” );
flush(stdin);                     //remove the garbage
gets(str);
l=strlen(str);             //count string length
printf(“\n the string is= “);
for(i=0;i<l;i++)
{
if(str[i]>65 && str[i]<91)
{
printf(“%c”,str[i]+32);
}
else
{
printf(“%c”,str[i]-32);
}
//print character by character in capital letters.
 }
getch();
}

//note : loop ke sath %c hi lagega.

program to count vowel and consonants.

#include<stdio.h>
#include<string.h>
main()
{
char str[50];
int i, l, v=0, c=0;
clrscr();
printf("\n Enter the string= ");
flush(stdin);
gets(str);
l=strlen(str);
printf("\n the string is= ");
for(i=0;i<l;i++)
{
printf("%c",str[i]);
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]='o' || str[i]=='u')
{v=v+i;} //count vovel
else
{c=c+i;} //count consonants
}
printf("\n The total vovels are= %d",v);
printf("\n the total consonants= %d",c);
getch();
}
//right button and show function details. (function directroy);

program to copy one string to another.


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[50], str2[50];
clrscr();
printf("\n Enter the string one");
gets(str1);
strcpy(str2,str1); //copy string1 into string2
printf("\n The string one is =%s",str1);
printf("\n The string two is =%s", str2);
getch();
}

program to copy one string to another string character by character using for loop.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[50], str2[50];
int i, l;
clrscr();
printf("\n Enter the string one= ");
fflush(stdin);
gets(str1);
l=strlen(str1);
printf("\n The string one is= %s",str1);
printf("\n The string two is=");
for(i=0;i<l;i++)
{
str2[i]=str1[i];
printf("%c",str2[i]);
}
getch();
}

No comments:

Post a Comment

Autonomous Vehicles

Autonomous Vehicles Autonomous vehicles are cars or trucks that can drive themselves without needing a human to control them. They use advan...