0

I'm new to C, and I'm having a bit of trouble figuring out the exact way to do this.

I need to iterate through a string and store each letter one at a time in order to decrypt it.

So what I'm doing is:

#1. Creating a place to store the string:

char toDecrypt[] = node->string;

#2. Starting the for loop:

for(int m=0; m< strlen(toDecrypt); ++m)

#3. Storing the char (to be decrypted later):

char i = toDecrypt[m];

So is the above valid, or should I be using a different notation to properly store the char?

EDIT:

Ok, I think I have that cleared up, so I just have one follow up question.

How do I check to see if a char is a "\"? My check doesn't seem to be working.

When I put

toDecrypt[m] != '\';

into an if statement, it doesn't work...

4

3 回答 3

1

将您的变量定义为char *toDecrypt = node->string;

如果您愿意,您仍然可以使用[]符号来读/写它。

于 2013-04-01T18:23:21.527 回答
0
  • 创建一个存储字符串的地方:

您实际上已经有一个存储字符串的地方。node->string存储字符串就好了。您可以创建一个指向它的指针:

char *toDecrypt = node->string;

或者如果你想在某个地方复制它,你可以创建一个数组:

char toDecrypt[enough_space_for_the_string];

// or do it dynamically with:
//     char * toDecrypt = malloc(enough_space_for_the_string);
//     just don't forget to free() it later

strcpy(toDecrypt, node->string);
  • 如何检查字符是否为“\”?我的支票似乎不起作用。

反斜杠是 C 中的转义字符,因此如果要检查反斜杠,则需要使用正确的转义序列

toDecrypt[m] != '\\';
于 2013-04-01T18:42:55.287 回答
0

这是错误的:char toDecrypt[] = node->string;

您可以通过以下方式解决它:

char *toDecrypt = node->string;

或者

char *toDecrypt=(char*) malloc (strlen(node->string)+1);
strcpy(toDecrypt,node->string);
于 2013-04-01T18:32:47.063 回答