Hot answers tagged

3

The description of the GetString() function clearly states that it can return NULL on an error or an EOF. If you pass the return value to strlen() without checking, your program will crash. string s = GetString(); int stringlength = 0; if (s != 0) stringlength = strlen(s); This at least won't crash. I also observe that the code fragment: // ...


2

Actually, it's not always changing to 1 on the last line. The result is always dependent on the input to argv[1]. Specifically, the last number printed depends on where the first 'a' appears in the input string. Here's what's happening. In the last printf() call, strlen(argv[1]) is measured by the appearance of the end of string marker in the string. But, ...


1

string input = get_string("Text:"); //getting input from the user int len = strlen(input); //determining the length of the string int space = 0; //declaring a variable to count whitespaces for (int i = 0; i < len; i++) //declaring a for loop to check each element of the string { if (input[i] == ' ') { space++; ...


1

From man scanf (conversions): s Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.


1

You've hit on the cause, so bonus points for debugging it enough to isolate the issue! Now the explanation. Look at the for loop: for (int j = 0; j < strlen(k); j++) On each pass, it calculates strlen(k) and checks that j is less than that. Now, think about what happens when an 'a' is processed. It is converted to binary 0, which happens to also be ...


1

The problem lies in the last for loop. The for loop is controlled by strlen(pt), which is recalculated on every loop iteration. Inside the loop, the ASCII values of the letters in pt are converted to numbers from 0 to 25, but are not converted back to encoded letters. The effect is this. When an 'a' or 'A' is encoded, it is transformed to 0. It is ...


1

You should print uppercased name[0] if it's not a space and is either the first character or following a space. If it's the first character, there's no previous element, but conveniently, the || operator uses short-circuit evaluation. If left side is true, it won't bother evaluating its right side. Same for && if left side is false. Note that &&...


1

The problem lies in your for loop that converts the key from letters to numbers. It's a very subtle problem. Look at the for loop setup: for(int n=0; n < strlen(key); n++) The loop is going to continue until n is >= strlen(key), which is recalculated after each pass. But what determines strlen(key)? The strlen() function looks for the end of string ...


1

values[] is an array with strings in it, you need to index it to measure the length of the string in it. And you really shouldn´t declare "n" as a variable for your for-loop, name it old-fashioned "i", see why?


1

Two things. One. Not sure why you say "not in the CS50 library" when you then pasted in the exact function that is in the CS50 library. Two. You'd generally get a segfault with strlen if the string doesn't actually exist (it's NULL) at the time you call strlen(). Are you actually entering a string in the terminal in response to your call to GetString()? ...


Only top voted, non community-wiki answers of a minimum length are eligible