Strings – Theory and Programs [Learn to code]

What is String? What are inbuild functions for processing string and different programs on Strings.

  • 081. Print string in reverse,its length,in uppercase,lowercase and copy into another string
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
        char *s1,*s2;
        clrscr();
        printf("Enter string : ");
        scanf("%s",s1);
        printf("Reverse String : %s",strrev(s1));
        printf("Lenght: %d",strlen(s1));
        printf("String in UPPER CASE : %s",strupr(s1));
        printf("String in lower case : %s",strlwr(s1));
        strcpy(s2,s1);
        printf("nCopied string is...%s",s2);
        getch();
    }
  • 082. Write a program to accept a string and print no. of aphlabets, digits, special symbols present in it in C language
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    main()
    {
    	char *s;
    	int i,alphabets=0,digits=0,symbols=0;
    	clrscr();
    	printf("Enter string : ");
    	scanf("%s",s);
    	for(i=0;s[i]!='';i++)
     
    		if(isalpha(s[i]))
    			alphabets++;
    		else if(isdigit(s[i]))
    			digits++;
    		else
    			symbols++;
     
    	printf("nAphabets : %d",alphabets);
    	printf("nDigits   : %d",digits);
    	printf("nSymbols  : %d",symbols);
    	getch();
    }
  • 083. Write a program to accept two strings and compare them in C language
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
    	char *s1,*s2;
    	clrscr();
    	printf("Enter string 1 : ");
    	scanf("%s",s1);
    	printf("Enter string 2 : ");
    	scanf("%s",s2);
     
    	if(strcmp(s1,s2)>0)
    		printf("nString 1 is greater..");
    	else if(strcmp(s1,s2)<0)
    		printf("nString 2 is greater..");
    	else
    		printf("nStrings are equal..");
     
    	getch();
    }
  • 084. Write a program to accept two strings and concatenate them in C language
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    main()
    {
    	char *s1,*s2;
    	clrscr();
    	printf("Enter string 1 : ");
    	scanf("%s",s1);
    	printf("Enter string 2 : ");
    	scanf("%s",s2);
    	printf("nConcatinated string is...%s",strcat(s1,s2));
    	getch();
    }
  • 085. Write a program to print length of a given string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,len=0;
    	char *s;
    	clrscr();
    	printf("Enter the string....");
    	scanf("%s",s);
    	for(i=0;s[i]!='';i++)
    		len++;
    	printf("The lenth of the string is...%d",len);
    	getch();
    }
  • 086. Write a program to copy a string into another string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
    	for(i=0;s1[i]!='';i++)
    	{
    		s2[i]=s1[i];
    	}
    	s2[i]='';
    	printf("The copied string is...%s",s2);
    	getch();
    }
  • 087. Write a program to convert given string to UPPER CASE in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	char *s1;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
     
    	for(i=0;s1[i]!='';i++)
    	{
    		if((s1[i]>='a')&&(s1[i]<='z'))
    			s1[i]=s1[i]-32;
    	}
    	printf("Resultant string is...%s",s1);
    	getch();
    }
  • 088. Write a program to convert givern string to lower case in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i;
    	char *s1;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
     
    	for(i=0;s1[i]!='';i++)
    	{
    		if((s1[i]>='A')&&(s1[i]<='Z'))
    			s1[i]=s1[i]+32;
    	}
     
    	printf("Resultant string is...%s",s1);
    	getch();
    }
  • 089. Write a program to concatinate two strings in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,len=0,j;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
    	printf("Enter the string 2....");
    	scanf("%s",s2);
     
    	for(i=0;s1[i]!='';i++)
    	{
             len++;
    	}
    	j=0;
    	for(i=len;s2[j]!='';i++)
    	{
    		s1[i]=s2[j];
    		j++;
    	}
    	s1[i]='';
     
    	printf("Resultant string is...%s",s1);
    	getch();
    }
  • 090. Write a program to count no, of alphlabets, digits, special symbols in C language
     
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,alphabets=0,digits=0,symbols=0;
    	char *s;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s);
     
    	for(i=0;s[i]!='';i++)
    	{
    		 if(((s[i]>='A')&&(s[i]<='Z'))||((s[i]>='a')&&(s[i]<='z')))
    				alphabets++;
    		 else if((s[i]>='0')&&(s[i]<='9'))
    				digits++;
    		 else
    				symbols++;
    	}
    	printf("nAlphabets : %d",alphabets);
    	printf("nDigits    : %d",digits);
    	printf("nSymbols   : %d",symbols);
    	getch();
    }
  • 091. Write a program to compare two strings in C language
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s1);
    	printf("Enter the string 2....");
    	scanf("%s",s2);
     
    	for(i=0;s1[i]!='';i++)
    	{
    		 if(s1[i]!=s2[i])
    			{
    				if(s1[i]>s2[i])
    					printf("nString 1 is greater...");
    				else
    					printf("nString 2 is greater...");
    				getch();
    				exit(0);
    			}
    	}
    	printf("String are equal....");
    	getch();
    }
  • 092. Write a program to count all vowels present in the string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,vowel=0;
    	char *s;
    	clrscr();
    	printf("Enter the string 1....");
    	scanf("%s",s);
     
    	for(i=0;s[i]!='';i++)
    	{
    		if((s[i]=='A')||(s[i]=='E')||(s[i]=='O')||(s[i]=='U')||(s[i]=='I')||
    		  (s[i]=='a')||(s[i]=='e')||(s[i]=='o')||(s[i]=='u')||(s[i]=='i'))
    						vowel++;
    	}
    	printf("nVowels : %d",vowel);
    	getch();
    }
  • 093. Write a program to reverse the given string in C language
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int i,j,len=0;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string ....");
    	scanf("%s",s1);
    	for(i=0;s1[i]!='';i++)
    		len++;
    	j=0;
    	for(i=len-1;i>=0;i--)
    	{
    		s2[j]=s1[i];
    		j++;
    	}
    	s2[j]='';
    	printf("nReversed String : %s",s2);
    	getch();
    }
  • 094. Write a program to check if the given string is palindrome or not

    E.g. NITIN, LIRIL, MALAYALAM.

    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    main()
    {
    	int i,j,len=0;
    	char *s1,*s2;
    	clrscr();
    	printf("Enter the string ....");
    	scanf("%s",s1);
    	for(i=0;s1[i]!='';i++)
    		len++;
    	j=0;
    	for(i=len-1;i>=0;i--)
    	{
    		s2[j]=s1[i];
    		j++;
    	}
    	s2[j]='';
    	for(i=0;s1[i]!='';i++)
    		if(s1[i]!=s2[i])
    		{
    			printf("nNot a palindrome");
    			getch();
    			exit(0);
    	}
    	printf("Palindrome");
    	getch();
    }

094. Write a program to check if the given string is palindrome or not

E.g. NITIN, LIRIL, MALAYALAM.

#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
	int i,j,len=0;
	char *s1,*s2;
	clrscr();
	printf("Enter the string ....");
	scanf("%s",s1);
	for(i=0;s1[i]!='\0';i++)
		len++;
	j=0;
	for(i=len-1;i>=0;i--)
	{
		s2[j]=s1[i];
		j++;
	}
	s2[j]='\0';
	for(i=0;s1[i]!='\0';i++)
		if(s1[i]!=s2[i])
		{
			printf("\nNot a palindrome");
			getch();
			exit(0);
	}
	printf("Palindrome");
	getch();
}

093. Write a program to reverse the given string in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,j,len=0;
	char *s1,*s2;
	clrscr();
	printf("Enter the string ....");
	scanf("%s",s1);
	for(i=0;s1[i]!='\0';i++)
		len++;
	j=0;
	for(i=len-1;i>=0;i--)
	{
		s2[j]=s1[i];
		j++;
	}
	s2[j]='\0';
	printf("\nReversed String : %s",s2);
	getch();
}

092. Write a program to count all vowels present in the string in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,vowel=0;
	char *s;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s);
 
	for(i=0;s[i]!='\0';i++)
	{
		if((s[i]=='A')||(s[i]=='E')||(s[i]=='O')||(s[i]=='U')||(s[i]=='I')||
		  (s[i]=='a')||(s[i]=='e')||(s[i]=='o')||(s[i]=='u')||(s[i]=='i'))
						vowel++;
	}
	printf("\nVowels : %d",vowel);
	getch();
}

091. Write a program to compare two strings in C language

#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
	int i;
	char *s1,*s2;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s1);
	printf("Enter the string 2....");
	scanf("%s",s2);
 
	for(i=0;s1[i]!='\0';i++)
	{
		 if(s1[i]!=s2[i])
			{
				if(s1[i]>s2[i])
					printf("\nString 1 is greater...");
				else
					printf("\nString 2 is greater...");
				getch();
				exit(0);
			}
	}
	printf("String are equal....");
	getch();
}

090. Write a program to count no, of alphlabets, digits, special symbols in C language

 
#include<stdio.h>
#include<conio.h>
main()
{
	int i,alphabets=0,digits=0,symbols=0;
	char *s;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s);
 
	for(i=0;s[i]!='\0';i++)
	{
		 if(((s[i]>='A')&&(s[i]<='Z'))||((s[i]>='a')&&(s[i]<='z')))
				alphabets++;
		 else if((s[i]>='0')&&(s[i]<='9'))
				digits++;
		 else
				symbols++;
	}
	printf("\nAlphabets : %d",alphabets);
	printf("\nDigits    : %d",digits);
	printf("\nSymbols   : %d",symbols);
	getch();
}

089. Write a program to concatinate two strings in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i,len=0,j;
	char *s1,*s2;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s1);
	printf("Enter the string 2....");
	scanf("%s",s2);
 
	for(i=0;s1[i]!='\0';i++)
	{
         len++;
	}
	j=0;
	for(i=len;s2[j]!='\0';i++)
	{
		s1[i]=s2[j];
		j++;
	}
	s1[i]='\0';
 
	printf("Resultant string is...%s",s1);
	getch();
}

088. Write a program to convert givern string to lower case in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i;
	char *s1;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s1);
 
	for(i=0;s1[i]!='\0';i++)
	{
		if((s1[i]>='A')&&(s1[i]<='Z'))
			s1[i]=s1[i]+32;
	}
 
	printf("Resultant string is...%s",s1);
	getch();
}

087. Write a program to convert given string to UPPER CASE in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i;
	char *s1;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s1);
 
	for(i=0;s1[i]!='\0';i++)
	{
		if((s1[i]>='a')&&(s1[i]<='z'))
			s1[i]=s1[i]-32;
	}
	printf("Resultant string is...%s",s1);
	getch();
}

086. Write a program to copy a string into another string in C language

#include<stdio.h>
#include<conio.h>
main()
{
	int i;
	char *s1,*s2;
	clrscr();
	printf("Enter the string 1....");
	scanf("%s",s1);
	for(i=0;s1[i]!='\0';i++)
	{
		s2[i]=s1[i];
	}
	s2[i]='\0';
	printf("The copied string is...%s",s2);
	getch();
}