简体   繁体   中英

How do I write a program that calculates string length and number of times a white space appears in the string in C

I'm not very good at programming and I've been asked to make a program that uses two functions. The first calculates the length of the string in a array. The second counts the white spaces in that same string. Here is what I have, I know it's not very good.

#include <stdio.h>

char string[]={"To be or not to be – that is the question: Whether 'tis noble in the          mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of     troubles and, by opposing, end them. To die, to sleep"};

void (char stringLength( char string[ ])
{
    int x = 0;

    while(string+x)
        x++;

    return x;
}

void space ( int numSpace(char string[])) {

    int x;
    numSpace = 0;
    for(x = 0; x < strlen(string); x++) {
        if(x[string] == ' ') {
            (numSpace)++;

            return numSpace;
        }
    }

}

int main(void)
{
    printf("string : %s\n", x);
    printf("total number of blank spaces : %d\n", numSpace);

    return 0;
}

The char string [] is supposed to be: To be or not to be – that is the question: Whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles and, by opposing, end them. To die, to sleep" For some reason when I posted my code it all stayed on one line. I don't know how to fix that problem either. I"m really over my head here.

You should compile with all warnings and debug info, eg with

 gcc -Wall -g yourcode.c -o yourbin

(at least on Linux)

Your declaration void (char stringLength( char string[ ]) is non-sense. The first parenthesis is wrong. A result cannot be both void and char . A string length can be bigger than 255 (often, the maximal value of a char ie CHAR_MAX from <limits.h> header).

unsigned stringLength(char string[ ]) {
 unsigned x = 0;
 while(string[x] != '\0')
    x++;
 return x;
}

a string length is an unsigned ie non-negative number (actually it is a size_t , but let's pretend it is unsigned ).

Your test while(string+x) is wrong (will have an undefined behavior ). Pointer arithmetic is [nearly] always non-null when the pointer is non-null and the offset non-negative (actually, it will overflow and wrap to 0, but before that you'll get a segmentation violation ). You want to access the memory string[x]

Your for(x = 0; x < strlen(string); x++) loop is extremely inefficient, you are computing strlen(string) at every loop (so the complexity of your loop is O(N 2 ) instead of O(N) ). You could compute strlen(string) before, eg size_t l = strlen(string); for (x=0; x<l; x++) size_t l = strlen(string); for (x=0; x<l; x++) or more wisely notice that you should stop at the terminating zero byte: for(x = 0; string[x]!='\\0'; x++)

At last learn how to use the debugger (eg gdb yourbin on Linux) and run your program step by step to understand what is happening.

Also, take several hours to read good tutorial material on C programming. Practice a lot. It will become fun.

As I noted in the comments, this looks like someone took a working program and intentionally scrambled it. It doesn't take much to turn it back into a working program:

#include <stdio.h>

char string[]={"To be or not to be – that is the question: Whether 'tis noble in the          mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of     troubles and, by opposing, end them. To die, to sleep"};

int stringLength( char string[ ])
{
    int x = 0;

    while(string[x])
        x++;

    return x;
}

int space (char string[])
{
    int numSpace;
    int x;
    numSpace = 0;
    for(x = 0; x < strlen(string); x++) { // expensive to call strlen on each iteration
        if(x[string] == ' ') { // legal but unusual; string[x] is more sensible
            (numSpace)++;
        }
    }

    return numSpace;

}

int main(void)
{
    printf("string : %s\n", string);
    printf("length of string: %d\n", stringLength(string));
    printf("total number of blank spaces : %d\n", space(string));

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM