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 below 50\n");    
    scanf("%s",str);    
    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 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 : 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 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 or not 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

 589 total views,  1 views today

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.