String functions in C


1. C Program to Find the Frequency of Characters in a String

2. C Program to Reverse a String by Passing it to Function

Source Code

1:

#include <stdio.h>
int main(){
   char c[1000],ch;
   int i,count=0;
   printf("Enter a string: ");
   gets(c);
   printf("Enter a characeter to find frequency: ");
   scanf("%c",&ch);
   for(i=0;c[i]!='\0';++i)
   {
       if(ch==c[i])
           ++count;
   }
   printf("Frequency of %c = %d", ch, count);
   return 0;
}
Output
Enter a string: md. ahsan arif
Enter a frequency to find frequency: a
Frequency of a = 3
 
2: 

#include<stdio.h>
#include<string.h>
void Reverse(char str[]);
int main(){
    char str[100];
    printf("Enter a string to reverse: ");
    gets(str);
    Reverse(str);
    printf("Reversed string: ");
    puts(str);
 return 0;
}
void Reverse(char str[]){
    int i,j;
    char temp[100];
    for(i=strlen(str)-1,j=0; i+1!=0; --i,++j)
    {
        temp[j]=str[i];
    }
    temp[j]='\0';
    strcpy(str,temp);
}
 
Output
Enter a string to reverse: fira
Reversed string: arif
 

1 টি মন্তব্য:

  1. #include
    int main()
    {
    char c[100],b;
    int i, a=0;
    printf("Enter a string:");
    gets(c);
    printf("Enter a characeter to find frequency:");
    scanf("%c",&b);
    for(i=0;c[i]!='\0';++i)
    {
    if(b==c[i])
    ++a;
    }
    printf("Frequency of%c=%d",b,a);
    return 0;
    }

    উত্তরমুছুন