Hot answers tagged

4

string is a variable type defined by the CS50 library - it does not normally exist in C. Looking at the code for CS50.h will show this: /* * Our own data type for string variables. */ typedef char *string; This line could be read that a new type is defined, as a char *, called string. In other words, string is defined as exactly the same thing as a char ...


4

The function printf writes to the standard output stream (aka stdout). The stdout usually buffers the data and then flushes it at some point (usually when a newline character is written or when it's told to). You're not printing a newline after each recently-read character so the stdout buffers the recently-read character and then sleep is called, so ...


3

The question is hinting at you to think about why the array is initialized with a size of 12.


2

First in the struct definition, you are the declaring a pointer to a char (char*), so if you don't want to use GetString(), when you try to store something that is not a pointer on any of this elements most probably you will get a segmentation fault. To fix this an option is to declare the char* like this: typedef struct { char name[255]; ...


2

The problem is simple. You have created two string arrays. Each holds the 26 letter alphabet, one lower case, the other upper case. They are created in sequential memory. In this case, the UC array is first, followed immediately by the lc array. You then try to print them out as strings, using the base variable (i.e., uppercase and lowcase) as the ...


2

If you know in advance how big of a string you are willing to accept, you can put the limit into your scanf argument itself. Try this small program: #include <stdio.h> int main(void) { char words[15]; scanf("%14s", words); printf("%s\n", words); } You're telling scanf to only take in 14 characters (with the 15th reserved for the null ...


2

One way would be to interpret characters 'a' to 'z' as digits in a number with base 26, start at "aaaa" (representing 0), and then adding 1 to the last string. Add or subtract 'a' for conversion between character and "digit" value. Instead of storing the string as your state, you could also store the values, and convert to the string for passing to crypt ...


2

The error comes from here: for(int l=0; l<strlen(argv); l++) Think about it. strlen() wants a const char* or char*. You gave it argv, which is an array of the command line arguments. Because argv is an array, strlen will not accept it and throws an error. A simple fix would be to replace argv with key since you are going to be iterating over the key's ...


1

I guess you mean if (isalpha(word[i])), with word being a char[] or char*, and i being something like an int? Could you maybe clarify your question by adding the surrounding code to your question?


1

Create an array of char, or a pointer to char with enough heap memory allocated using malloc (remember strings require a termination character '\0', so are longer than their content by one character). Then you can assign to individual array elements. A few examples how one could use those: char *str = (char*)malloc((LENGTH+1)*sizeof(char)); // now str can ...


1

simple answer. You can't. That's because a string is a collection of sequential chars. Each char can be accessed as if it was in an array. For example, say you had string mystring. Then the third char in the string would be mystring[2].


1

Perhaps there's a slight oversight in your code. Look at the following: lc_word[len + 1] = '\0'; Now, say that the word is "Cat". Length is 3 so len = 3. Remember that arrays start at 0, not 1. So, "Cat" or "cat" would occupy 0, 1 and 2. The line of code above would put the end of string marker at lc_word[3+1], or lc_word[...


1

Character pointers are normally used to build literal strings, the most usual way is to make a statement of the following type: char *name = "my name is very long"; Some things you need to know about this statement. It is enclosed in double quotes. The pointer name points to the first elements of the string. It is read-only or constant, if we try to ...


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

Funny thing is that I can't get much to work right in cs50.h so I've just been looking up standard c programming techniques. It's just been much easier to find working examples on the Internet using standard c without cs50.h. for the life of me I can't get get_string() in cs50.h working even though I copied the example in the manual. This link has 3 great ...


1

The formula, as it is in the specs, and as you're using it, only works if the alphabetic chars are in a range 0 - 25. That is, 'a' == 0, 'b' == 1, etc. That's not the case because that's not the ASCII value of the alphabetic chars. You have to account for that. Get an ASCII table, look at the values the letters have, and then "translate" the ASCII value to ...


1

That's a non-alpha ASCII value that is attempting to be printed. If you really want to know what it is, try printing it as an integer. Something along the lines of printf("the char ascii value is %i\n", name[0]). However, you have another issue. You declared name as a char array without giving it a size and without initializing it with data when declared. ...


1

You have just placed the parenthesis in the wrong place. It shouldn't be if(strcmp(temp, thisNode->word == 0)) But if(strcmp(temp, thisNode->word) == 0) The condition thisNode->word == 0 will always return false which is equal to 0. So you are comparing temp which is a string with 0 which is an int. If this answers your question please ...


1

Your problem is much more basic and simple. Look at these lines: char x; // a char variable char *y; // a pointer to a string y = "a"; // loads a string into memory at y if (x == y){ // compares a char and a string y is a pointer to a string, not a character. You are attempting to compare a char to a pointer. There are two ...


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