Saturday, August 18, 2018

PRINT INDEX


  • Problem Description

    How to find all occurrences of a character in a given string using for loop in C programming. Program to print all index of a character in a given string.
  • CODING ARENA
  • #include<stdio.h>
    #include<string.h>
    int main()
    {
      char a,word[50];
      int i=0;
      scanf("%c",&a);
      scanf("%s",word);
      for(i=0;i<strlen(word);i++)
      {
        if(word[i]==a)
        {
          
          printf("%c is found at index %d\n",a,i);
          
        }
      }
      return 0;
    }


             
      
  • Test Case 1

    Input (stdin)
    u
    
    srmuniversity
    
    
    
    
    Expected Output
    u is found at index 3
  • Test Case 2

    Input (stdin)
    e
    
    srmuniversitylearningcentre
    
    
    Expected Output
    e is found at index 7
    
    e is found at index 14
    
    e is found at index 22
    
    e is found at index 26

No comments:

Post a Comment