0

I have a problem as stated in the title. Here more details.

My Problem is:

a) develop a C-function which gets a char array as input parameteter and which determines the largest substring in this char array without 'e' 'E'. Print the length of that substring.

b) write main function to test a) function. use scanf("%[^\n]",...) or a loop with getchar()

EX: input : "you are one." .output : "you ar" length: 6

3 Answers3

2

If you use the string library function strtok the solution is very straight forward. Using this function it pull the substrings out for you and then you just keep track of the longest one.

temp = strtok(input, "eE");
strcpy(best, temp);
bestLen = strlen(best);

while(temp = strtok(NULL, "eE")) {
    if(strlen(temp) > bestLen) {
        strcpy(best, temp);
        bestLen = strlen(best);
    }
}
printf("%s", best);

strtok will split your string every time there is an e or E and return that into your temp pointer. You can then look at the temp value and see if it is long enough to be your new best length.

Bmoore
  • 315
  • 4
  • 16
0
#include<stdio.h>
int main(){
char myStr[30],Str[30];
char ch;
int i = 0, j=0;
gets(myStr);


while(myStr[j] != '\0'){  //Use this to determine any 'e' or 'E' inside the string
   if(myStr[j] == 'e' || myStr[j] == 'E'){
          j++;
          continue;
   } else{
          Str[i] = myStr[j];
          i++;
   }
   j++;
}
printf("%s  length:%d",Str,i--);
return 0;
}
whalesf
  • 49
  • 1
  • 8
0

Improvement of answer form @ChartesL.:

#include<stdio.h>
int main(){
    char myStr[30];
    char *ptr;
    // read myStr from console
    ...
    char* delim = myStr;
    int longest = 0;
    char*longestSubStr = null;

    for(ptr=myStr; *ptr != '\0'; ptr++) 
    {  
       if(*ptr == 'e' || *ptr == 'E') 
       {
           int substrLength = ptr - delim;
           if(substrLength  > longest)
           {
               longest = substrLength;
               longestSubstr = delim;
           }
           delim = ptr+1;
       }      
    }
    // show longest length in longest
    // longest substring starts at longestSubStr, first 'longest' chars
    ...

    return 0;   
}

it remebers the last delimiter (either start of string, "e" or "E") and whenever a new delimiter is found, the length of the substring between two delimiters is calculated. Then determinest the longest of these lengths and remembers the longest substring found so far.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • it print only the character after the final 'e' or 'E' – MINH PHUNG DO Mar 25 '15 at 03:40
  • i understand the algorithm but i just started to learn 3 days ago. could you please to write the whole code completely. i'm very appreciated that. thank you. – MINH PHUNG DO Mar 25 '15 at 03:55
  • how can i print out the substring and the length of it. – MINH PHUNG DO Mar 25 '15 at 04:07
  • Sorry, but this is too basic to be answered her. Read any book about c to learn this. – DrKoch Mar 25 '15 at 04:13
  • i know, i use gets(myStr) to input. and printf("output %s," longestSubStr). printf(" %d", substrLength). to printout the longest substring and the length. But when i run "aeaae" the output is also "aeaae", and the length just count the character in front of the first 'e' – MINH PHUNG DO Mar 25 '15 at 04:18
  • 1
    Yes. As notetd in the code snippet, your resulting string **starts at** `longestSubStr`. Find a way to print the first `longest` chars of this string only. Hint: you may use `strncpy()` – DrKoch Mar 25 '15 at 04:20