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,900 questions

51,831 answers

573 users

How to find the length of the shortest word in a string with C

3 Answers

0 votes
#include <stdio.h>
#include <string.h>
 
int main() {
    char string[128] = "C is a general purpose procedural computer programming language";
    int shortest = 99999;
    char *token, delimeter[2] = " ";
 
    token = strtok(string, delimeter);
 
    while(token != NULL) {
        if(shortest > strlen(token)){
            shortest = strlen(token);
        }
        token = strtok(NULL, delimeter);
    }
 
    printf("%d", shortest);
 
    return 0;
}
 
 
 
      
/*
run:
  
1
      
*/

 



answered Sep 16, 2021 by avibootz
0 votes
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int is_separator(char c) {
    return c == ' ' || c == '\t' || c == '\n';
}

int shortest_word_length(const char *str) {
    int min_len = 0;
    int current_len = 0;

    for (int i = 0; ; i++) {
        char c = str[i];

        if (c == '\0' || is_separator(c)) {
            if (current_len > 0) {
                if (min_len == 0 || current_len < min_len) {
                    min_len = current_len;
                }
                current_len = 0;
            }
            if (c == '\0')
                break;
        } else {
            current_len++;
        }
    }

    return min_len;
}

int main(void) {
    const char *text = "Find the shortest word length in this string";

    int result = shortest_word_length(text);

    printf("Shortest word length: %d\n", result);

    return 0;
}



/*
run:
  
Shortest word length: 2
  
*/

 



answered 21 hours ago by avibootz
0 votes
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int shortest_word_length(const char *str) {
    char buffer[1024];
    strncpy(buffer, str, sizeof(buffer));
    buffer[sizeof(buffer) - 1] = '\0';  // ensure null-termination

    int min_len = 0;

    // Tokenize by spaces, tabs, and newlines
    char *token = strtok(buffer, " \t\n");

    while (token != NULL) {
        int len = strlen(token);

        if (min_len == 0 || len < min_len) {
            min_len = len;
        }

        token = strtok(NULL, " \t\n");
    }

    return min_len;
}

int main(void) {
    const char *text = "Find the shortest word length in this string";

    int result = shortest_word_length(text);

    printf("Shortest word length: %d\n", result);

    return 0;
}



/*
run:
  
Shortest word length: 2
  
*/

 



answered 21 hours ago by avibootz
...