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

51,918 answers

573 users

How to print the length of each words in a string with C

2 Answers

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

int main(void)
{
    char string[] = "c c++ java python php javascript c#";
    char delimiters[] = " ";

    char* p = strtok(string, delimiters);

    while (p) {
        printf("%s = %zu\n", p, strlen(p));
        p = strtok(NULL, delimiters);
    }

    return 0;
}




/*
run:

c = 1
c++ = 3
java = 4
python = 6
php = 3
javascript = 10
c# = 2

*/

 



answered May 13, 2022 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s[] = "c c++ java python javascript c# php";
    
    int size = strlen(s);
    int endIndex, startIndex = 0;

    for (int i = 0; i < size; i++) {
        if (s[i] == ' ' || i == size - 1) {
            endIndex = i;
            if (i == size - 1) endIndex = size;
            printf("%.*s = %d\n", (endIndex - startIndex), s + startIndex, (endIndex - startIndex));
            startIndex = i + 1;
        }
    }

    return 0;
}




/*
run:

c = 1
c++ = 3
java = 4
python = 6
javascript = 10
c# = 2
php = 3

*/

 



answered May 13, 2022 by avibootz

Related questions

1 answer 155 views
1 answer 143 views
1 answer 100 views
1 answer 146 views
1 answer 156 views
2 answers 225 views
3 answers 260 views
...