Wednesday, February 3, 2021

STRING PROGRAM IN C

[1]finding length of a String without using library function strlen

 #include <stdio.h>

int main()
{
    
    char str[100],i;
    printf("Enter a string: \n");
    scanf("%s",str);

    // '\0' represents end of String
    for(i=0; str[i]!='\0'; ++i);
       printf("\nLength of input string: %d",i);
    
    return 0;
}

[2]Program to separate the individual characters from a string

#include <stdio.h>  
#include <string.h>  
   
int main()  
{  
    char string[] = "characters";  
          printf("Individual characters from given string:\n");  
      
    //Iterate through the string and display individual character  
    for(int i = 0; i < strlen(string); i++){  
        printf("%c ", string[i]);  
    }  
          
    return 0;  
[3]Write a program in C to print individual characters of string in reverse order
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
    char str[100]; /* Declares a string of size 100 */
    int l,i;
       printf("Input the string : ");
       fgets(str, sizeof str, stdin);
   l=strlen(str);
   printf("The characters of the string in reverse are : \n");
       for(i=l;i>=0;i--)
        {
          printf("%c  ", str[i]);
        }
    printf("\n");
}

Program to Count Total Number of Words in a String

  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5. char a[100];
  6. int len,i,word=1;
  7. clrscr();
  8. printf("\nENTER A STRING: ");
  9. gets(a);
  10. len=strlen(a);
  11. for(i=0;i<len;i++)
  12. {
  13. if(a[i]!=' ' && a[i+1]==' ')
  14. word=word+1;
  15. }
  16. printf("\nTHERE ARE %d WORDS IN THE STRING",word);
  17. getch();
  18. }

C Program to Compare Two Strings without using strcmp

Output:

Enter two strings :ac
acde
s1 is less than s2

 Program to Count Alphabets Digits Special characters in a string


OUTPUT : :


No comments:

Post a Comment