Home > Blockchain >  Scanning and Printing using scanf and printf
Scanning and Printing using scanf and printf

Time:11-30

I have this counting substring palindrome code and I want to scanf the string first and then print the count of the total of palindrome in the substring

I tried using %i and %d and it just showed main error

#include <stdio.h>
#include <string.h>


int countSubstrings(char * s)
{
    int res = 0;
    int left = 0;
    int right = 0;
    int len = strlen(s);
    for (int i = 0; i < len; i  ) {
        left = i;
        right = i;
        while (left >= 0 && right < len && s[left] == s[right]) {
            res  ;
            left--;
            right  ;
        }
        left = i;
        right = i   1;
        while (left >= 0 && right < len && s[left] == s[right]) {
            res  ;
            left--;
            right  ;
        }
    }
    
    
}

int main ()
{
    int res = 0;
    int len = strlen(s);
    scanf ("%i", &len);
    printf ("%d", res);

}

Expected Input:

(words)

Expected output:

(the count of substring)

CodePudding user response:

Modify the main function as follows:

int main (){
     char s[100];  // If the maximum length of input is < 100.
     scanf("%s",s);// It will scan standard input till whitespace
     printf("%d",countSubstrings(s));
}
  •  Tags:  
  • c
  • Related