Alternating Characters

  • + 0 comments

    yeah . Mine is O(n).

    try and analyze this. I'm giving you the source code.

    include

    include

    include

    include

    int main() { int i,j,c=0,x,k; char a[100]; scanf("%d",&i);

    while(i--)
        { c=0;            //refreshing the counter
       scanf("%s",a);
        x=strlen(a);       //length of string
        for(j=0;j!=x-1;j++)   //no need to traverse till end                                 //but end-1
            {
    
            if(a[j] ==a[j+1])     //calc. those which are same
              {  c++;
    
              }
    
        }
        printf("%d\n",c);
    
    }
    
    return 0;
    

    }