Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

How to find the largest and the smallest word in a string with C

3 Answers

0 votes
#include <stdio.h> 
#include <string.h> 
#include <ctype.h>
 
void find_largest_and_smallest_word_in_string(char string[], char min[], char max[]) {
    char word[16];
    int j = 0, flag = 0;
  
    for (int i = 0; i < strlen(string); i++) {
        while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i])) {
            word[j++] = string[i++];
        }
  
        if (j != 0) {
            word[j] = '\0';
  
            if (!flag) {
                flag = !flag;
                strcpy(max, word);
                strcpy(min, word);
            }
  
            if (strlen(word) > strlen(max)) {
                strcpy(max, word);
            }
  
            if (strlen(word) < strlen(min)) {
                strcpy(min, word);
            }
  
            j = 0;
        }
    }
 }

int main(void)
{   
    char string[] = "c cpp c# python java";
    char min[16] = "", max[16] = "";
    
    find_largest_and_smallest_word_in_string(string, min, max);
    
    printf("The largest word is: %s\n", max);
    printf("The smallest word is: %s\n", min);
      
    return 0;
}

   
         
/*
run:
      
The largest word is: python
The smallest word is: c
 
*/

 



answered Mar 18, 2017 by avibootz
edited Jun 2, 2024 by avibootz
0 votes
#include <stdio.h> 
#include <string.h> 

void find_largest_and_smallest_word_in_string(char string[], char min[], char max[]) {
    char word[16];
    int j = 0;
  
    strcpy(min, string);
      
    for (int i = 0; i < strlen(string); i++) {
        if (string[i] != ' ') {
            word[j++] = string[i];
        }
        else {
            if (strlen(word) > strlen(max)) {
               strcpy(max, word);
            }
            if (strlen(word) < strlen(min)) {
                strcpy(min, word);
            }
            word[0] = '\0';
            j = 0;
        }
    }
 }
 
int main(void)
{   
    char string[] = "c cpp c# python java";
    char min[16] = "", max[16] = "";
     
    find_largest_and_smallest_word_in_string(string, min, max);
     
    printf("The largest word is: %s\n", max);
    printf("The smallest word is: %s\n", min);
       
    return 0;
}
 
    
          
/*
run:
       
The largest word is: python
The smallest word is: c
  
*/

 



answered Mar 22, 2017 by avibootz
edited Jun 2, 2024 by avibootz
0 votes
#include <stdio.h> 
#include <string.h> 

void find_largest_and_smallest_word_in_string(char string[], char min[], char max[]) {
    strcpy(min, string);
   
    char *word = strtok(string, " ,.-");
 
    while (word != NULL) {
        if (strlen(word) > strlen(max)) {
            strcpy(max, word);
        }
        if (strlen(word) < strlen(min)) {
            strcpy(min, word);
        }
         
        word = strtok(NULL, " ,.-");
    }
 }
 
int main(void)
{   
    char string[] = "c cpp c# python java";
    char min[16] = "", max[16] = "";
     
    find_largest_and_smallest_word_in_string(string, min, max);
     
    printf("The largest word is: %s\n", max);
    printf("The smallest word is: %s\n", min);
       
    return 0;
}
 
    
          
/*
run:
       
The largest word is: python
The smallest word is: c
  
*/

 



answered Jun 2, 2024 by avibootz
edited Jun 2, 2024 by avibootz

Related questions

...