C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
16. Program to convert all characters in a string to lower case using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
 // Converting to lower case
 strlwr(str);
 
 // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

17. Program to convert all characters in a string to lower case without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, ch=character
 char str[50], ch;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
  
 // Converting to lower case
 for(i=0;str[i]!='\0';i++)
    {
        ch=str[i];
        
        if(ch>=65 && ch<97)
        {
            ch+=32; // ch1=ch1+32
            str[i]=ch;
        }
    }
    
    // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}
18. Program to convert all characters in a string to upper case using string function

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
 // Converting to lower case
 strupr(str);
 
 // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

19. Program to convert all characters in a string to upper case without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, ch=character
 char str[50], ch;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
  
 // Converting to lower case
 for(i=0;str[i]!='\0';i++)
    {
        ch=str[i];
        
        if(ch>=97 && ch<=122)
        {
            ch-=32; // ch1=ch1-32
            str[i]=ch;
        }
    }
    
    // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

20. Program to enter 5 string and print them with their length
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 /* Declaring variable str[5][50]- This means that 5 string is to be input 
 with max size of each string = 50 */
 char str[5][50];
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 for(i=0;i<5;i++)
 {
  printf("Enter %d string : ", i+1);
  gets(str[i]);
 }
 
 // Printing string with its length
 for(i=0;i<5;i++)
 {
  printf("\nString %d : %s ",i+1, str[i]);
  printf("\nString length : %d",strlen(str[i]));
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

Share this

Related Posts

Previous
Next Post »