Showing posts with label string. Show all posts
Showing posts with label string. Show all posts
C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
21. Program to accept a string and print each word of the string separately also print total number of words
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, word=to store the word
 char str[50], word[50];
 
 /* Declaring variable cw=count words, i=to iterate loop, 
 j=works as index position of word */
 int cw=0, i, j=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 /* We are concatinating a space here assuming user does not 
 give space at the last */
 strcat(str," ");

 // Printing string
 printf("Entered string : %s",str);
 
 //Determing total number of words and displaying it
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]!=' ')
  {
   word[j++]=str[i];
  }
  else
  {
   word[j]='\0';
   printf("\nWord : %s",word);
   cw++;
   j=0;
  }
 }
 
 // Printing total words
 printf("\nTotal number of words : %d",cw);
 
 getch(); // Linux user - Remove this

 return 0;
}

22. Program to accept a string and display vowels frequency( total number of vowels)
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 /* Declaring variable cv=count vowel, i=to iterate loop 
 ( i works as index position) */
 int cv=0, i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Checking if vowels and counting them
 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')
  {
   cv++;
  }
 }
 
 // Printing total number of vowels
 printf("Entered String : %s",str);
 printf("\nTotal number of vowels : %d",cv);
 
 getch(); // Linux user - Remove this

 return 0;
}

23. Program to accept a string and display frequency of each vowel along with vowel
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Declaring variable ca=count vowel a
 int ca=0, ce=0, ci=0, co=0, cu=0;
 
 // Declaring variable i=to iterate loop ( i works as index position)
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Determining frequency of each vowel
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]=='a' || str[i]=='A')
  ca++;
  
  if(str[i]=='e' || str[i]=='E')
  ce++;
  
  if(str[i]=='i' || str[i]=='I')
  ci++;
  
  if(str[i]=='o' || str[i]=='O')
  co++;
  
  if(str[i]=='u' || str[i]=='U')
  cu++;
  
 }
 
 // Printing string
 printf("Entered String : %s",str);
 
 // Printing frequency of string
 printf("\nFrequency of 'a' or 'A' : %d",ca);
 printf("\nFrequency of 'e' or 'E' : %d",ce);
 printf("\nFrequency of 'i' or 'I : %d",ci);
 printf("\nFrequency of 'o' or 'O' : %d",co);
 printf("\nFrequency of 'u' or 'U' : %d",cu);
 
 getch(); // Linux user - Remove this

 return 0;
}

24. Program to enter a word and check whether it is palindrome or not using string function
// palindrom - When word = reverse of word then it is called palindrome
// Example : madam - palindrome, Madam - not palindrome, MadaM - palindrome

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

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char word[50], word1[50];
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // Copying word to word1
 strcpy(word1,word);
 
 // checking if palindrome or not
 if(strcmp(word,strrev(word1))==0)
 {
  printf("Entered word is a palindrome ");
 }
 
 else
 printf("Entered word is not palindrome ");
 
 getch(); // Linux user - Remove this

 return 0;
}


25. Program to enter a word and check whether it is palindrome or not without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char word[50], revword[50];
 
 /* Declaring variable i=to iterate loop, l=length, 
 c=count the number of matched character */
 int i, j, l=0, c;
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // finding length
 while(word[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revword[j]=word[i];
  j++;
 }
 revword[j]='\0';
 
 //checking if palindrome or not
 c=0;
 for(i=0;word[i]!='\0';i++)
 {
  if(revword[i]==word[i])
  c++;
 }
 
 if(c==l)
 {
  printf("Word is a palindrome ");
 }
 
 else
 printf("Word is not palindrome ");
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : STRINGS

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;
}

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
10. Program for Compare two String using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[50], str2[50];
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Comparing string
 if(strcmp(str1,str2)==0)
 {
  printf("Same string or string are equal");
 }
 
 else if(strcmp(str1,str2)<0)
 {
  printf("First string is smaller ");
 }
 
 else if(strcmp(str1,str2)>0)
 {
  printf("Second string is smaller ");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program for Compare two String without using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
  // Declaring variable str1=string1, str2=string2
  char str1[50],str2[50];
  
  // Declaring variable ch1=character of string1, ch2=character of string2
  char ch1,ch2;
  
  // Declaring variable i=to iterate loop, flag = to indicate comparision
     int i,flag=0;
               
        // Inputing string       
        printf("\nEnter First String : " );
        gets(str1);
        
        printf("\nEnter Second String : " );
     gets(str2);
     
     // Comparing string
  for(i=0;str1[i]!='\0';i++)
        {
            ch1=str1[i];
            ch2=str2[i];
            if(ch1>=97 && ch1<=122)
            {
                ch1-=32; // ch1=ch1-32
            }
            if(ch2>=97 && ch2<=122)
            {
                ch2-=32; // ch2=ch2-32
            }
            if(ch1!=ch2)
            {
                flag=1;
                break;
            }
        }
               if(flag==0)
                   {
                        printf("\nString  '%s' and  '%s' are equal ",str1,str2);
                   }
               else
                   {
                        printf("\nString  '%s' and  '%s' are Not equal ",str1,str2);
                   }

 
 getch(); // Linux user - Remove this

 return 0;
}

12. Program for String Reverse with 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 and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",strrev(str));
 
 getch(); // Linux user - Remove this

 return 0;
}

13. Program for String Reverse without String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string, revstr[50]=reverse string
 char str[50], revstr[50];
 
 // Declaring variable i=to iterate loop, l=length
 int i, j,l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // finding length
 while(str[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revstr[j]=str[i];
  j++;
 }
 revstr[j]='\0';
 
 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",revstr);
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program for String Concatenation with String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[50], str2[50];
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Concatinating string
 strcat(str1,str2); // str2 is added to str1
 
 // Printing conactinated string
 printf("Concatinated string : %s\n",str1);
 
 getch(); // Linux user - Remove this

 return 0;
}

15. Program for String Concatenation without String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[100], str2[50];
 
 // Declaring variable l=length of string1
 int l1=0;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Finding length of string1 and string2
 while(str1[l1]!='\0')
 l1++;
 
 // Concatinating string
 for(i=0;i<str2[i]!='\0';i++)
 {
  str1[l1++]=str2[i];
 }
 str1[l1]='\0';

 // Printing Concatinated stirng
  printf("Concatinated string : %s\n",str1);
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
6. Program to find length of a string 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];
 
 // Declaring variable l=length
 int l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Determining string length
 l=strlen(str);
 
 // Printing string length
 printf("String : %s\nString length : %d",str,l);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to find length of a string without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Declaring variable l=length
 int l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Determining string length
 while(str[l]!='\0')
 {
  l++;
 }
 
 // Printing string length
 printf("String : %s\nString length : %d",str,l);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program for Copy a String to another using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string , cs=copied string
 char str[50], cs[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Copy string
 strcpy(cs,str);
 
 // Printing string
 printf("String : %s\n",str);
 printf("Copied string : %s",cs);
 
 getch(); // Linux user - Remove this

 return 0;
}

9. Program for Copy a String to another without using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string , cs=copied string
 char str[50], cs[50];
 
 // Declaring variable l=length
 int l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 while(str[l]!='\0')
 {
  cs[l]=str[l];
  l++;
 }
 cs[l]='\0';
 
 // Printing string
 printf("String : %s\n",str);
 printf("Copied string : %s",cs);
 
 getch(); // Linux user - Remove this

 return 0;
}


C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
1. Program to accept a string and print it
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=to store string inputted by user
 char str[80];
 
 // Inputing string
 printf("Enter any sentence : ");
 gets(str);
 
 // printing string
 printf("\nEntered String : ");
 puts(str);
 
 // Another way of printing string
 printf("\nEnterd string : %s",str);
  
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to accept a character in the uppercase and print in lower case
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 //Declaring variable ch to hold character value and cl=change to lower case 
 char ch,cl;

 // Inputting character
 printf("Enter a character in uppercase : ");
 ch=getchar();
 
 // Converting it to lower case
 cl=ch+32;
 
 // Printing lower case character
 printf("The given character in lowercase is : %c \n",cl);
 
 // Another way of priting
 printf("The given character in lowercase is : ");
 putchar(cl);
 
 getch(); // Linux user - Remove this

 return 0;
}

3. Program to accept a character in Lower case and print it in upper case
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable ch=character, cu=change character to upper case
 char ch,cu; 
 
 // Entering character
 printf("Enter a charcater in lower case: ");
 scanf("%c",&ch);
 
 // Converting it to upper case
 cu=ch-32;
 
 // Printing upper case character
 printf("The given character in upper case is : %c \n",cu);
 
  getch( ); // Linux user - Remove this

  return 0;
}


4. Program to accept a character in any case and print in another case
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable ch to hold character value and cch=change character
 char ch,cch;

 // Inputting character
 printf("Enter a character in anycase : ");
 scanf("%c",&ch);

 // Changing case
 if(ch>=65 && ch<=90)
 cch=ch+32;
 
 else if(ch>=97 && ch<=122)
 cch=ch-32;
 
 // Printing Changed case
 printf("Changed character : %c ",cch);
 
 getch(); // Linux user - Remove this

 return 0;
}


5. Program to accept a string and print it by using while loop
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable ch to store input character
 char ch; 

 // Inputing and displaying string
 printf("Enter a string : ");
 while(( ch=getchar( ))!='\n')
 putchar(ch); 

 getch(); // Linux user - Remove this

 return 0;
}

COMMON PROGRAMMING ERRORS - STRING

COMMON PROGRAMMING ERRORS - STRING

COMMON PROGRAMMING ERRORS - STRING
  • Not allocating sufficient space in a character array to store the null character that terminates a string.
  • Printing a "string" that does not contain terminating null character.
  • Processing a single character as a string. A string is a pointer - probably a respectably large integer. However, a character is a small integer ( ASCII values range 0-255). On many systems this causes an error, because low memory address are reserved for special purposes such as operating system interrupt handlers. So "access violation" occurs.
  • Passing a character as an argument to a function when a string is expected (and vice versa) is a compilation error.
  •  Not including the <string.h> header when using string functions from the string-handling library.
  • Not appending a terminating null character to the first argument of a strncpy when the third argument is less than or equal to the length of the string in the second argument.
More Informative Posts: