Example : A program in C to display respective ASCII value of given character.
#include<stdio.h>
#include<conio.h>  
void main() 
{    
    char x;
    
    printf("Enter single character from keyboard to see its respective ASCII Value =");
    scanf("%c",&x);
    printf("%d",x);	
      
   getch();
} 
Example : A program in C to display respective ASCII character from given single ASCII value from 0-255.
#include<stdio.h>
#include<conio.h>  
void main() 
{    
    int i;
    printf("Enter single value between 0-255 to see the respective ASCII character =");    
	scanf("%d",&i);
	printf("%c",i);	
      
   getch();
} 
Example : A string program in C to store and display static string value.
#include<stdio.h>
#include<conio.h>  
void main() 
{    
    //char str[]="Codershelpline";
    //char str[50]="Codershelpline";
    //char str[] = {'C','o','d','e','r','s','h','e','l','p','l','i','n','e','\0'};
    char str[15] = {'C','o','d','e','r','s','h','e','l','p','l','i','n','e','\0'};    
    printf("%s",str); 
      
   getch();
} 
Example : A string program in C to store and display dynamic string value.
#include<stdio.h>
#include<conio.h>
void main() 
{     
    char str[50];
    printf("Enter String Value upto 50\n");    
    scanf("%s",str);    
    printf("%s",str);
  
   getch();
} 
Example : A string program in C to find the length of character in a given string without using string function.
#include<stdio.h>
#include<conio.h>

void main() 
 {    
    char str[30];int i,charcount=0;
    printf("Enter String Value upto 30 characters = ");
    gets(str);
    for(i=0; str[i]!='\0'; i++)
    {	  
        charcount++;
    }   
        
    printf("The total no. of character in a given string is = ");
    printf("%d",charcount);
    
    getch(); 
 }
Example : A string program in C to display the string in reverse order without using string function.
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() 
 {    
    char str[50];
    int i,length;
    printf("Enter String Value upto 50 characters = ");
    gets(str);
    length=strlen(str);

    for(i=length; i>=0; i--)
    {	  
         printf("%c  ", str[i]);
    } 
    getch(); 
 }
----------------------------------  OR  ----------------------------------
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() 
 {    
    char str[50];
    int i,length;
    printf("Enter String Value upto 50 characters = ");
    gets(str);
    length=strlen(str);
    for(str[length]='\0'; length>=0; length--)
    {	  
         printf("%c  ", str[length]);
    } 
    getch(); 
 }
Example : A string program in C to convert a lowercase string value into its respective uppercase value without using string function.
#include<stdio.h>
#include<conio.h>
void main() 
{     
    char str[50];int i;
    printf("Enter String Value upto 50 character\n");    
    scanf("%s",str);
	  
	for(i=0;str[i]!='\0';i++)
	{
	  if( str[i] >= 'a' && str[i] <= 'z' )
            {
              str[i] = str[i] - 32;
            }
	}  
    printf("%s",str);
  
   getch();
} 
---------------------------------  OR  ---------------------------------
#include<stdio.h>
#include<conio.h>
void main() 
{     
    char str[50];int i;
    printf("Enter String Value upto 50 character\n");    
    scanf("%s",str);
	  
	for(i=0;str[i]!='\0';i++)
	{
	  if( str[i] >= 97 && str[i] <= 122 )
           {
             str[i] = str[i] - 32;
           }
	}  
    printf("%s",str);
  
   getch();
} 

NB : ASCII value of A=65 & Z=90 and a=97 and z=122
Example : A string program in C to convert a uppercase string value into its respective lowercase value without using string function.
#include<stdio.h>
#include<conio.h>
void main() 
{     
    char str[50];int i;
    printf("Enter String Value upto 50 character\n");    
    scanf("%s",str);
	  
	for(i=0;str[i]!='\0';i++)
	{
	  if( str[i] >= 'A' && str[i] <= 'Z' )
           {
             str[i] = str[i] + 32;
           }
	}  
    printf("%s",str);
  
   getch();
} 
-----------------------------------  OR  -----------------------------------
#include<stdio.h>
#include<conio.h>
void main() 
{     
    char str[50];int i;
    printf("Enter String Value upto 50 character\n");    
    scanf("%s",str);
	  
	for(i=0;str[i]!='\0';i++)
	{
	  if( str[i] >= 65 && str[i] <= 90 )
           {
             str[i] = str[i] + 32;
           }
	}  
    printf("%s",str);
  
   getch();
} 
Example : A string program in C to store and display static string value with the help of user defined function.
#include<stdio.h>
#include<conio.h>
void main() 
 {    
    char str[]="Codershelpline";    
    strfun(str);     
    getch(); 
 }
    void strfun(char str1[]) 
    { 
    	printf("String value is : %s",str1); 
    }
Example : A string program in C to store and display dynamic string value with the help of gets() and puts() method.
#include<stdio.h>
#include<conio.h> 

void main() 
 {    
    char name[30];
    printf("Enter your name: ");
    gets(name);     
    printf("The entered Name is: ");
    puts(name);      
    getch(); 
 }

--------  OR  --------
#include<stdio.h>
#include<conio.h>
void main() 
 {    
    char name[30];
    printf("Enter your name: ");
    gets(name);
    printf("The entered Name is: ");
    puts(name);
    
    printf("Enter your name: ");
    fgets(name, sizeof(name), stdin);	     
    printf("The entered Name is: ");
    puts(name);
	      
    getch(); 
 }
Output :
Enter your name: Codershelpline
The entered Name is: Codershelpline
Enter your name: Codershelpline
The entered Name is: Codershelpline
Example : How to  copy one string value into another in C without using string function.
#include <stdio.h>
int main()
{
    //char str1[50], str2[50], i;
    char str1[50], str2[50];
    int i;

    printf("Enter First String = ");
    scanf("%s",str1);
    
    for(i = 0; str1[i] != '\0'; i++)
    {
        str2[i] = str1[i];
    }
    str2[i] = '\0';
    printf("The Copied String is = %s", str2);
    return 0;
}
Example : How to  count total no. of space and word present in a given string.
#include<stdio.h>
#include<conio.h>

void main() 
 {    
    char str[30];int i,space=0,wordcount=0;
    printf("Enter String Value upto 30 characters = ");
    gets(str);
    for(i=0; str[i]!='\0'; i++)
    {      
      if(str[i]==' '&& str[i+1]!=' ')
	  {	  	
	    space++;
	    wordcount++;
	  }   
    }    
    printf("The total no. of space and word in a given string is = ");
    printf("%d and %d",space,wordcount+1);
    
    getch(); 
 }
Example : How to  count total no. of lines of text present in a given string.
#include<stdio.h>
#include<conio.h>

void main() 
 {    
    char str[300];int i,line=0;
    printf("Enter Multi line String Value by pressing enter button for line upto 300 characters terminated with ~ symbol = ");    
   	scanf("%[^~]", str);
   	
    for(i=0; str[i]!='\0'; i++)
    {      
      if(str[i]=='\n')
      {
         line++;         
      }
    }    
    printf("The total no. of lines in given string are = ");
    printf("%d ",line);
    
    getch(); 
 }

OUTPUT :
Enter Multi line String Value by pressing enter button for line upto 300 characters terminated with ~ symbol = A
B
C
D
E
F
~
The total no. of lines in given string are = 6
Example : A string program in C to read and display string value with the help of pointer.
#include <stdio.h>
#include <conio.h>
int main()
{
    char str[50];
    char *ptr;
     
    printf("Enter a string value: ");
    gets(str);    
    ptr=str;   
     
    printf("Entered string is: ");
    while(*ptr!='\0')
      {
        printf("%c",*ptr++);
      }    
    return 0;
}
Example : A string program in C to display static string value with the help of pointer.
#include<stdio.h>
#include<conio.h> 

void main() 
 {    
    char str[]="Coders Helpline";

  	printf("%c\t",*str);     // Output: C
  	printf("%c\t",*(str+2));   // Output: d
  	printf("%c\t\n\n",*(str+4));   // Output: r

  	char *ptr;
  	ptr=str;
  	printf("%c\t",*ptr);     // Output: C
  	printf("%c\t",*(ptr+1));   // Output: o
  	printf("%c\t",*(ptr+7));   // Output: H    
      
    getch(); 
 }
Example : A string program in C to count and display no. of characters present in a given string.
/*  ---------------------------  Without String Function --------------------------------*/
#include<stdio.h>
#include<conio.h>
void main()
{
    char str[50];
    int i,count=0;

    printf("Enter a string value below 50 characters: ");
    scanf("%s",str);
	
    for(i=0;str[i]!='\0';i++)
    count++;
    printf("Length of a given string is %d",count);
    
    getch();
}  
Example : A string program in C to check, count and display no. of vowels, consonants, numbers, blank spaces, symbols and other characters present in a sentence of string.
#include<stdio.h>
#include<conio.h>
void main()
{
    char str[100];
    int i,countv,countc,countd,countb,counts;
    countv=countc=countd=countb=counts=0;

    printf("Enter a string value: ");
    scanf("%[^\n]", str);    

    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]=='a' || str[i]=='A' || str[i]=='e' ||
           str[i]=='E' || str[i]=='i' || str[i]=='I' ||
           str[i]=='o' || str[i]=='O' || str[i]=='u' ||
           str[i]=='U')
        {
            countv++;
        }
        else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
        {
            countc++;
        }
        else if(str[i]>='0' && str[i]<='9')
        {
            countd++;
        }
        else if (str[i]==' ')
        {
            countb++;
        }
        else
        {
            counts++;	
	}
    }
    printf("Total no. of vowels are : %d",countv);
    printf("\nTotal no. of Consonants are: %d",countc);
    printf("\nTotal no. of Digits are: %d",countd);
    printf("\nTotal no. of White/blank spaces are : %d", countb);
    printf("\nTotal no. of symbols & other characters are : %d", counts);

    getch();
}

NB : Here scanf("%[^\n]",str) is used in place of scanf("%s",str) because earlier is a kind of regular expression that matches a filled/nonempty sequence of characters from the received scanset of characters and read all the user inputs/larger character set/multiple word input until  we reached/added (\n) newline or pressed enter button/key and finally store it into the given variable of sufficient length.
Example : A string program in C to check the given string is palindrome or not .
#include <stdio.h>
#include <string.h>

void main()
{
    char str[20];
    int i,len;
    int status=0;
    
    printf("Enter a string value below 20 letters:");
    scanf("%s",str);
    
    len=strlen(str);
    
    for(i=0;i<len;i++)
	{
        if(str[i]!=str[len-i-1])
		{
            status = 1;
            break;
        }
    }
    
    if(status) 
	{
        printf("%s is NOT a palindrome word", str);
    }    
    else 
	{
        printf("%s is a palindrome word", str);
    }
    
    return 0;
}

/*  ------------------------------   OR  ----------------------------------*/

#include<stdio.h>
#include<conio.h> 
void main()
{       
    int len,i=0,j,status=0;    
    char str[40];    
    printf("Enter a string/letter below 40 character: ");
    scanf("%s",str);
    
    j=strlen(str)-1;    
    while(i<=j)
    {
        if (str[i]!=str[j])
        {
            status=1;
            break;
        } 
		       
        i++;  
        j--;
    }
    
    if(status)
    {
        printf("%s is NOT Palindrome word", str);
    }
    else
    {
        printf("%s is Palindrome word", str);
    }
    
    getch();
}

/*  ------------------------------   OR  ----------------------------------*/

#include <stdio.h> 
#include <conio.h>
int main() 
{ 
    palindrome("madam"); 
    palindrome("mam"); 
    palindrome("codershelpline");
    palindrome("dalda");
    palindrome("racecar");	 
    return 0; 
}   

void palindrome(char str[]) 
{ 
    int len = 0; 
    int h = strlen(str)-1;  
    
    while (h>len) 
    { 
        if (str[len++]!=str[h--]) 
        { 
            printf("%s is Not Palindrome word\n", str); 
            return; 
        } 
    } 
    printf("%s is palindrome word\n", str); 
} 

NB : Palindrome word or number is a sequence of character that reads the same forward as backward. For example - NOON, NO LEMON NO MELON, RACE CAR, MADAM, 121, 151, EYE,RADAR, LEVEL,REFER, REVIVER, CIVIC, DEIFIED, MOM, ROTATOR, WOW etc.  
Example : A string program in C to check the given string is palindrome without using function.
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
    char str1[40],str2[40];
    int status;
    
    printf("Enter a string value upto 40 characters: ");
    gets(str1);

    strcpy(str2,str1);
    strrev(str2);

    status=stricmp(str1,str2);
    
    if(status==0)
    {
        printf("The Accepted String %s is Palindrome",str1);
    }
    else
    {
        printf("The Accepted String %s is Not Palindrome",str1);
    }

    return 0;
}
Example : A C program to copy the contents of one string into another using  without string function.
#include <stdio.h>
#include<conio.h>
void main()
{
    char name1[20],name2[20];
    int i;
    printf("Enter First String : ");
   	scanf("%s",name1);
   	for(i=0;name1[i]!='\0';i++)
   	{
   	   name2[i]=name1[i];
	}
	   name2[i]='\0';
   	printf("copied string is= %s",name2);
   	getch();
}
Example : Write a string program in C to create the given pattern.
#include<stdio.h>
#include<conio.h>
void main()
	{
	   int i, j,len=0,len1;
	   char str[20];
	   clrscr();
	   printf("\n Enter String Value : = ");
	   scanf("%s",str);
	   len=strlen(str);
		
	   for(i=0;i<len;i++)
		{
		   for(j=0;j<=i;j++)
		   {
		      printf("%c",str[j]);					
		   }
		   printf("\n");
		}			
			
		len1=len;
		for(i=0;i<len;i++)
		{
		   len1=len1-1;
		   for(j=0;j<=len1;j++)
		   {
			printf("%c",str[j]);
		   }
		   printf("\n");						
	        }
	        getch();
	}

Output :
I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I

/*-------------   OR   ------------*/

# include <stdio.h>
#include<conio.h>
main()
{
	int x, y;	
	static char str[ ] = "INDIA";
	printf("\n");
	for(x=0;x<5;x++)
	{
	   y = x + 1;	
	/* Here the value 5 reserves 5 character of space on to the monitor screen and minus 
           sign is for left justified*/
	
	   printf("%-5.*s \n", y, str);
	
	/* for every loop execution the * is replaced by value of y */
	/* y value starts with 1 and for every time it is incremented by 1 until it reaches to 
           5*/
	}
	for( x=4; x>=0; x--)
	{
	   y = x + 1;
	   printf("%-5.*s \n", y, str);	
  /* y value starts with 5 and for every time it is decrements by 1 until it reaches to 1*/
	}
    getch();
}
Output :
I
IN
IND
INDI
INDIA
INDIA
INDI
IND
IN
I
 /*------------------- OR ----------------------------------*/

#include<stdio.h>
#include<conio.h>
main()
{
	int x, y;	
	static char str[ ] = "INDIA";
	printf("\n");
	for(x=0;x<5;x++)
	{
	   y = x + 1;	
	/* Here the value 5 reserves 5 character of space on to the monitor screen and minus 
           sign is for left justified*/
	
	   printf("%-5.*s \n", y, str);
	
	/* for every loop execution the * is replaced by value of y */
	/* y value starts with 1 and for every time it is incremented by 1 until it reaches to 
           5*/
	}
	for( x=4; x>=0; x--)
	{
	   y = x + 1;
	   printf("%-5.*s \n", y, str);	
  /* y value starts with 5 and for every time it is decrements by 1 until it reaches to 1*/
	}
    getch();
}

Output :
I
IN
IND
INDI
INDIA
INDI
IND
IN
I
Example : A String program in C to store three string values in an array of strings and display them on to the monitor screen.
#include<stdio.h>
#include<conio.h>
main()
{
	int i;
	char names[3][6] = {"Ram", "Rahim", "Gita" };
	for(i=0; i<3; i++)
	printf("%s \n",names[i] );
	getch();
}

Output :
Ram
Rahim
Gita
Example : A String program in C to count the number of characters present in a given pointer String using User Defined Function.
int mystrlen( char *st )
{
   char *ptr2;
   int count = 0;

   for( ptr2 = st; *ptr2 != '\0'; ptr2++ ) 
   	count++;
   return count;
}
int main()
{
	char *ptr1 = "Welcome";
	int cnt;
	cnt=mystrlen(ptr1);
	printf("%d",cnt);
	return 0;
}

Output :
7

Loading

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.