All String program in C Language Program examples


Content 



1. Count string without built in function

#include<stdio.h>
#include<conio.h>
void main()
{
int len=0;
char s[10];
clrscr();
printf("Enter a string: ");
gets(s);
while(s[len]!='\0')
{
len++;
}
printf("\nLength:%d",len);
getch();
}

Output:

Enter a string: Welcome
Length:7




2. count words in string

#include<stdio.h>
#include<conio.h>
void main()
{
int len=0,c=1;
char s[40];
clrscr();
printf("Enter a string:");
gets(s);
while(s[len]!='\0')
{
if(s[len]==' ')c++;
len++;
}
printf("\nTotal words:%d",c);
getch();
}

Output:

Enter a string:How are you
Total words:3






3. Replace character in string

#include<stdio.h>
#include<conio.h>
void main()
{
int len=0,i;
char s[40],c,r;
clrscr();
printf("Enter original string:");
gets(s);
while(s[len]!='\0')len++;
fflush(stdin);
printf("\nEnter character to replace:");
scanf("%c",&c);
fflush(stdin);
printf("\nEnter new character:");
scanf("%c",&r);
for(i=0;i<len;i++)
{
if(s[i]==c)
{
s[i]=r;
}
}
puts(s);
getch();
}

Output:

Enter original string:how are you
Enter character to replace:o
Enter new character:k
hkw are ymu






4. Toggled charecter in string

#include<stdio.h>
#include<conio.h>
void main()
{
int len=0,i;
char a[40],b[40];
clrscr();
printf("Enter orignal string:");
gets(a);
while(a[len]!='\0')
len++;
for(i=0;i<len;i++)
{
       if(a[i]>'A'&&a[i]<='Z')
{
b[i]=a[i]+32;
}
       elseif(a[i]>='a'&&a[i]<='z')
{
       b[i]=a[i]-32;
}
       else
{
b[i]=a[i];
}
}
printf("\ntoggled string:%s",b);
getch();
}

Output:

Enter orignal string:HOw Are YoU
toggled string:hoW aRE yOu






5. Get string from user without displaying  it on console)

#include<stdio.h>
int main()
{
char str[100],ch;
int n,count=0;
printf("\nEnter String : ");
  while(1)
  {
  ch=getch();
  if(ch==13)//13 is ASCII value of \n
  {
  break;
  }
  str[count]=ch;
  count++;
  }
printf("\nEntered String : %s",str);
   return 0;
}

Output:

Enter String : Programming
Entered String : Programming






6. delete character from string

#include<stdio.h>
#include<conio.h>
void main()
{
int len=0,i,f,k=0;
char s[40],d,store[40];
clrscr();
printf("enter orignal string:");
gets(s);
while(s[len]!='\0')
{
len++;
}
fflush(stdin);
printf("enter character to delete:");
scanf("%c",&d);
for(i=0;i<len;i++)
{
if(s[i]!=d)
{
store[k++]=s[i];
}
}
puts(store);
getch();
}

Output:

enter orignal string:how are you
enter character to delete:o
hw are yu






7. reverse string example

#include<stdio.h>
#include<conio.h>
void main()
{
int len,i,j;
char s1[20],s2[20];
clrscr();
printf("Enter orignal string :");
gets(s1);
len=0;
while(s1[len]!='\0')
{
len++;
}
for(i=0,j=len-1;i<len;i++,j--)
{
s2[j]=s1[i];
}
s2[len]='\0';
printf("\nreverse string :%s",s2);
getch();
}

Output:

Enter orignal string:hello
reverse string:olleh






8. Check String Is Palindrome or Not

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20];
int i,j,flag=0;
clrscr();
printf("Enter string:");
gets(s);
for(i=0,j=strlen(s)-1;i<strlen(s)/2;i++,j--)
{
if(s[i]!=s[j])
{
flag=1;
break;
}
}
if(flag==0)
printf("\nPalindrome");
else
printf("\nNot palindrome");
getch();
}

Output:

Enter string:malayalam
Palindrome







9. Show ASCII value of given String's Character

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char name[20];
clrscr();
printf("Enter A string :>>");
gets(name);
for(i=0;i<strlen(name);i++)
{
printf("%c\t%d\n",name[i],name[i]);
}
getch();
}

Output:

Enter A string:hello
h              104
e               101
l                108
l                108
o               111





10. Convert String into Upper String

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
int i=0;
clrscr();
printf("Enter a string :>>");
gets(s1);
while(s1[i]!='\0')
{
if(s1[i]>=97&&s1[i]<=122)
{
s2[i]=s1[i]-32;
}
else
{
s2[i]=s1[i];
}
i++;
}
s2[i]='\0';
printf("\n\n Upper case string:>>%s",s2);
getch();
}

Output:

Enter a string:>>helLO
Upper case string:>>HELLO






11. Convert String Into Lower String

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
int i=0;
clrscr();
printf("Enter a string :>>");
gets(s1);
while(s1[i]!='\0')
{
if(s1[i]>=65&&s1[i]<=90)
{
s2[i]=s1[i]+32;
}
else
{
s2[i]=s1[i];
}
i++;
}
s2[i]='\0';
printf("\n Lower Case string :%s",s2);
getch();
}

Output:

Enter a string:>>HELlo
Lower Case string:hello






12. Check the word Is In Given String or Not

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[80],s2[20];
int len1=0,len2=0,i,j,found=0;
clrscr();
printf("Enter the string:>>");
gets(s1);
while(s1[len1]!='\0')len1++;
printf("\n enter the word to find:>>");
gets(s2);
while(s2[len2]!='\0')len2++;
for(i=0;i<len1;i++)
{
found=1;
for(j=0;j<len2;j++)
{
if(s1[i+j]!=s2[j])
{
found=0;
break;
}
}
if(found==1)
{
printf("\n\n\t string found");
break;
}
}
if(found==0)
{
printf("\n\n\t string not found");
}
   getch();
}

Output:

Enter the string:>>how are you
enter the word to find:>>how
string found





13. Print The Pattern Of Given String

#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0;
char s[40];
clrscr();
printf("\n\n\t\t Enter the word  U want to Format:>>");
gets(s);
for(i=0;s[i]!='\0';i++)
{
for(j=0;j<=i;j++)
{
printf("%c",s[j]);
}
printf("\n");
}
getch();
}

Output:

Enter the word  U want to Format:>>hello
h
h e
h e l
h e l l
h e l l o





14. Delete A Given Word From A Given String

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[80],s2[20],store[40];
int len1=0,len2=0,i,j,k,found;
clrscr();
printf("Enter the string:>>");
gets(s1);
while(s1[len1]!='\0')
{
len1++;
}
printf("\n enter the word to Delete:>>");
gets(s2);
while(s2[len2]!='\0')
{
len2++;
}
for(i=0;i<len1;i++)
{
found=1;
store[i]=s1[i];
       for(j=0;j<len2;j++)
{
if(s1[i+j]!=s2[j])
{
found=0;
break;
}
}
  if(found==1)
  {
printf("\n string found");
for(k=i-1,j=i+len2;j<=len1;j++,k++)
{
store[k]=s1[j];
}
puts(store);
break;
}
}
  if(found==0)
  {
printf("word to be deleted not found");
  }
  getch();
}

Output:

Enter the string:>>how are you
enter the word to Delete:>>are
string found
how you






15. String Functions

#include <stdio.h>
void main()
{
     int count1 = 0, count2 = 0, flag = 0, i;
     char string1[10], string2[10];
     printf("Enter a string:");
     gets(string1);
     printf("Enter another string:");
     gets(string2);
     /*  Count the number of characters in string1 */
     while (string1[count1] != '')
         count1++;
     /*  Count the number of characters in string2 */
     while (string2[count2] != '')
         count2++;
     i = 0;
 
     while ((i < count1) && (i < count2))
     {
         if (string1[i] == string2[i])
         {
             i++;
             continue;
         }
         if (string1[i] < string2[i])
         {
             flag = -1;
             break;
         }
         if (string1[i] > string2[i])
         {
             flag = 1;
             break;
         }
     }
     if (flag == 0)
         printf("Both strings are equal ");
     if (flag == 1)
         printf("String1 is greater than string2 ", string1, string2);
     if (flag == -1)
         printf("String1 is less than string2 ", string1, string2);
}

Output:

Enter a string: hello
Enter another string: world
String1 is less than string2
 
Enter a string:object
Enter another string:class
String1 is greater than string2
 
Enter a string:object
Enter another string:object
Both strings are equal







16. Calculate the total number of capital, small and special Character from Given String

#include<stdio.h>
#include<conio.h>
void main()
{
char m[40];
int cap=0,small=0,spec=0,i;
clrscr();
printf("Enter a string:>>");
gets(m);
for(i=0;i<strlen(m);i++)
{
if(m[i]>='A'&&m[i]<='Z')
{
cap++;
}
else if(m[i]>='a'&&m[i]<='z')
{
small++;
}
else
{
spec++;
}
}
printf("\ncapital letters:%d\n",cap);
printf("small letters:%d\n",small);
printf("special letters:%d\n",spec);
getch();
}

Output:

Enter a string:>>how ARE you 123
capital letters:3
small letters:6
special letters:5





17. Arrange String in the Alphabetical order

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char str[10],tmp;
int i,j,len=0;
clrscr();
printf(" ENTER ANY STRING [MAX 10 CHARACTERS]:\n");
gets(str);

len=strlen(str);
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{

if(str[i]>str[j])
{
tmp=str[j];
str[j]=str[i];
str[i]=tmp;
}
}
}
printf("STRING IN THE ALPHABETICAL ORDER IS:\n");
puts(str);
getch();
}

Output:

ENTER ANY STRING [MAX 10 CHARACTERS]:
hello
STRING IN THE ALPHABETICAL ORDER IS:
ehllo






18. Check Character of String How many time repeted

#include<stdio.h>
#include<conio.h>
void main()
{
char str[50],i,j,a,b=0;
clrscr();
printf("Enter any name:>>");
gets(str);
a=strlen(str);
for(i=0;i<a;i++)
{
for(j=0;j<a;j++)
{
if(str[i]==str[j])
b++;
}
printf("%c=%d",str[i],b);
b=0;
       printf("\n");
}
       getch();
}

Output:

Enter any name:>>Hello
H=1
 e=1
 l=2
 l=2
 o=1




19. Append String Example

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100],s2[10];
int len1=0,len2=0,i,j;
clrscr();
printf("Enter first string:");
fflush(stdin);
gets(s1);
printf("\nEnter second string:");
fflush(stdin);
gets(s2);
while(s1[len1]!='\0')
{
len1++;
}
while(s2[len2]!='\0')
{
len2++;
}
for(j=0,i=len1;j<=len2;i++,j++)
{
s1[i]=s2[j];
}
puts(s1);
getch();
}

Output:

Enter first string:hello
Enter second string:welcome
hellowelcome





20. Count Vowel From Given String

#include<stdio.h>
#include<conio.h>
void main()
{
int len=0,i,vowel=0;
char arr[100],ch;
clrscr();
printf("Enter any string :");
fflush(stdin);
gets(arr);
while(arr[len]!='\0')
{
len++;
}
for(i=0;i<=len;i++)
{
ch=arr[i];
if(ch=='A'||ch=='E'||ch=='U'||ch=='O'||ch=='I'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowel++;
}
}
printf("Total vowel is=%d",vowel);
getch();
}

Output:

Enter any string:hEllo how are yOu
Total vowel is=6



21. Copy String

#include<stdio.h>
#include<conio.h>
void main()
{
char arr[100],copy[100];
int len=0,i;
clrscr();
printf("Enter the string :");
fflush(stdin);
gets(arr);
while(arr[len]!='\0')
{
len++;
}
for(i=0;i<=len;i++)
{
copy[i]=arr[i];
}
printf("Copy String:");
puts(copy);
getch();
}

Output:

Enter the string:hello
Copy String:hello




22. C Program to Input 2 Binary Strings and Print their Binary Sum

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
int bin_verify(char []);
void sum(char [], char [], char []);
 
int main()
{
     char bin1[33], bin2[33], result[33];
     int len1, len2, check;
 
     printf("Enter binary number 1: ");
     scanf("%s", bin1);
     printf("Enter binary number 2: ");
     scanf("%s", bin2);
     check = bin_verify(bin1);
     if (check)
     {
         printf("Invalid binary number %s.", bin1);
         exit(0);
     }
     check = bin_verify(bin2);
     if (check)
     {
         printf("Invalid binary number %s.", bin2);
         exit(0);
     }
     sum(bin1, bin2, result);
     printf("%s + %s = %s", bin1, bin2, result);
 
     return 0;
}
 
int bin_verify(char str[])
{
     int i;
 
     for (i = 0; i < strlen(str); i++)
     {
         if ((str[i] - '0' != 1 ) && (str[i] - '0' != 0))
         {
             return 1;
         }
     }
 
     return 0;
}

  void sum(char bin1[], char bin2[], char result[])
{
     int i = strlen(bin1) - 1;
     int j = strlen(bin2) - 1;
     int carry = 0, temp, num1, num2;
 
     while (i > -1 && j > -1)
     {
         num1 = bin1[i] - '0';
         num2 = bin2[j] - '0';
         temp = num1 + num2 + carry;
         if (temp / 2 == 1)
         {
             carry = 1;
             temp %= 2;
         }
         if (i > j)
         {
             result[i + 1] = temp + '0';
             result[strlen(bin1) + 1] = '';
         }
         else
         {
             result[j +1] = temp + '0';
             result[strlen(bin2) + 1] = '';
         }
         i--;
         j--;
     }
     while (i > -1)
     {
         temp = bin1[i] + carry - '0';
         if (temp / 2 == 1)
         {
             carry = 1;
             temp %= 2;
         }
         result[i + 1] = temp + '0';

         i--;
     }
     while (j > -1)
     {
         temp = bin2[j] + carry - '0';
         if (temp / 2 == 1)
         {
             carry = 1;
             temp %= 2;
         }
         result[j + 1] = temp + '0';
         j--;
     }
     if (carry)
     {
         result[0] = '1';
     }
     else
     {
         result[0] = '0';
     }
}

Output:

Enter binary number 1: 0110
Enter binary number 2: 1011
0110 + 1011 = 10001


Reactions

Post a Comment

0 Comments