c-strings

How to check if a char contains a specific letter

人走茶凉 提交于 2019-12-24 05:56:37
问题 I'm trying to produce code that will check how many "A", "C", "G", and "T" characters a char contains. But I'm a little unsure how to go about doing this.. because as far as I know there aren't any operators like .contains() to check with. The desired output would be something like: "The char contains (variable amount) of letter A's" In my code I have this right now: DNAnt countDNAnt(char strand) { DNAnt DNA; if (isupper(strand) == "A") { DNA.adenine++; } else if (isupper(strand) == "C") {

Partition a 1D char* into 2D char**

人走茶凉 提交于 2019-12-24 03:15:21
问题 There are a lot of questions about converting a 2D array into a 1D array, but I am attempting just the opposite. I'm trying to partition a string into substrings of constant length and house them in a 2D array. Each row of this 2D matrix should contain a substring of the initial string, and, if each row were to be read in succession and concatenated, the initial string should be reproduced. I nearly have it working, but for some reason I am losing the first substring (partitions[0] -- length

Strange std::cout behaviour with const char*

大憨熊 提交于 2019-12-23 18:55:45
问题 I have a method which returns a string to display as an error message. Depending on where this error occurs in the program, I might add a bit more of an explanation to the error message before displaying it. string errorMessage() { return "this is an error"; } // somewhere in the program... const char* message = ("Some extra info \n" + errorMessage()).c_str(); cout << message << endl; (I am storing the message as a const char* since I will actually provide this error to another method which

C error:format '%s' expects argument of type 'char *'but argument 2 has type 'char (*)[100]'

ⅰ亾dé卋堺 提交于 2019-12-23 12:53:53
问题 I'm working on an exercise in c the last few days and I'm having this warning (as the title suggests). I've tried a bunch of stuff but I don't really know how to exactly fix this. I'm not good at programming so there are mistakes. Below are the structs I'm using (which cannot be changed because that's how they are given): typedef struct bookR* book; struct bookR{ char author[MAXSTRING]; enum genres{fiction,scientific,politics}; int id; char review[MAXLINES][MAXSTRING]; }; typedef struct nodeR

Initialization of an array of structs in C++

坚强是说给别人听的谎言 提交于 2019-12-23 05:35:09
问题 If I have a struct like below: typedef struct MyStruct { char **str; int num; } MyStruct; Is there a way for me to initialize an array of this structures. Perhaps like below: const MyStruct MY_STRUCTS[] = { { {"Hello"}, 1 }, { {"my other string"}, 3 }, }; Ultimately I would like to have a constantly declared array of structs inside a C++ class. How can this be done? Is it possible to have a privately declared member that is pre-initialized? 回答1: Sure, you'd write it like this: #include

Is there anyway to create null terminated string in Go?

耗尽温柔 提交于 2019-12-22 03:51:44
问题 Is there anyway to create null terminated string in Go? What I'm currently trying is a:="golang\0" but it is showing compilation error: non-octal character in escape sequence: " 回答1: Spec: String literals: The text between the quotes forms the value of the literal, with backslash escapes interpreted as they are in rune literals (except that \' is illegal and \" is legal), with the same restrictions. The three-digit octal ( \nnn ) and two-digit hexadecimal ( \xnn ) escapes represent individual

NSString cString is Deprecated. What is the alternative?

风格不统一 提交于 2019-12-21 09:21:07
问题 I've got another newbie question. I've written a piece of code that converts a NSString to NSMutableData in order to simulate a webService result. It turns out however that cString is deprecated. Can you help me replace it? Here's my code. NSString *testXMLDataString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" etc.... "</SOAP-ENV:Envelope>"; //Replace webData Received from the web service with the test Data NSMutableData *testXMLData = [NSMutableData dataWithBytes:[testXMLDataString

C - char array and char pointer

℡╲_俬逩灬. 提交于 2019-12-21 05:25:10
问题 Why I can't define an array char **pp={ "123", "456", "789" }; But I can define it as a char*[] ,and send it to a function that will accept it as a char ** char *pp[]={ "123", "456", "789" }; fun(pp); void fun(char **pointerToPointer) { //++(**pointerToPointer);//error printf("%s", *pointerToPointer); } //output::"123" And why I can't increment ++(**pointerToPointer); 回答1: To answer the first question, the principles might be clearer if we use a single depth of pointer. This code is illegal

Crash in CAtlStringMgr::GetInstance under Windows XP

不想你离开。 提交于 2019-12-21 05:01:20
问题 I've written a DLL that creates an ATL CString object. I compile it with Visual Studio 2015 using the "Visual Studio 2015 - Windows XP (v140_xp)" platform toolset. The DLL is loaded using LoadLibrary/GetProcAddress. It crashes under Windows XP in CAtlStringMrg::GetInstance when allocating the string object. The same application works well on Windows Vista and later. Here is the disassembly: static IAtlStringMgr* GetInstance() { #pragma warning(push) #pragma warning(disable: 4640) static

Does garbage collection happen when we initialize a char array with a string literal in c?

被刻印的时光 ゝ 提交于 2019-12-20 06:27:58
问题 When we write the following line of code in C, char local_arr[] = "I am here"; the literal "I am here" gets stored in the read only part of the memory(say RM ). How I visualize it is that it gets stored contiguously in the RM (Is that right?). Then the array local_arr (i.e local array) copies this array index by index from its location in RM. But what happens to the literal after the local_array copies it? Is it lost thereby causing memory leaks? Or is there some sort of garbage collector