c - Issue with scanf(...) and fgets(...) -
i'm doing alternating characters problem.
problem:
shashank likes strings in consecutive characters different. example, likes ababa, while doesn't abaa. given string containing characters , b only, wants alter string likes. this, allowed delete characters in string.
your task find minimum number of required deletions.
input format first line contains integer t i.e. number of test cases. next t lines contain string each.
output format print minimum number of required steps each test case.
constraints
1≤t≤10 1≤ length of string ≤105
my code:
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { /* come in code here. read input stdin. print output stdout */ int t,i,j; scanf("%d", &t); char line[100000]; for(i=0; < t; i++){ //get string user fgets(line, sizeof(line), stdin); //loop line , greedily solve int deletions = 0; if(strlen > 0){ char currentchar = line[0]; for(j=1; j < strlen(line); j++){ if(currentchar == line[j]){ deletions++; }else{ currentchar = line[j]; } } } printf("%d\n", deletions); //empty string memset(line,0,strlen(line)); } homecoming 0; } input:
5 aaaa bbbbb abababab bababa aaabbb my output:
0 3 4 0 0 expected output:
3 4 0 0 4 i'm not c expert, i'm sorry if obvious question. seems fgets catches empty line when shouldn't. guess was related scanf. however, i'm not sure why , i'm not sure how prepare it?
there new line character in file right after '5' ignore, should be:
scanf("%d\n", &t); that of course of study assuming have unix-file (e.g. \n new line)
c
No comments:
Post a Comment