English 中文(简体)
C. 脑
原标题:Manipulating strings in C

<>分> 这是大学的工作。 我并不期望守则能完成我的任务,而是想理解如何最好地操纵C的扼杀。

我需要书写某种功能,以某种方式操纵扼杀(我不说什么,以确保没有人提供准确的代码)。

In python I d just do the following

def foo(str):
  return str

Clearly things aren t as easy as that in C.

Can anyone tell me how best to achieve this. Should I use pointers to simulate passing by reference and just manipulate the original string? Any help / resources would be greatly appreciated.

<>Update: 我确实想预示着这一行动的结果(也是一种扼杀)。 我很高兴地操纵原来的扼杀或归还。 以往将被视为最佳做法。

The task I ve been set is based on how to do that operation so I didn t want to make that explicit.

因此,沙尔将:

def foo(str):
  #do something to str (which doesn t change it s length)
  return str
最佳回答

有两个途径:

  1. If you manipulate the original string you don t need to return anything
  2. If you don t want to change the original string you should copy it with strdup or malloc + strcpy and return a pointer (char *) to it. The caller of course must free it when done.

所有这一切都取决于问题:,打电话者需要原封顶?

问题回答

有许多公约,但最常见的公约是:

void foo(char *destination, const char *source);

or

void foo(char *destination, size_t dest_len, const char *source);

页: 1 (该职能不会修改)产出说明载于<编码>stination/code>。

Of course, the caller needs to make sure that destination is of a sufficient size, otherwise the call results in undefined behaviour.

这一工作应在C:

char* foo(char *str)
{
  return str;
}

或者说:

void foo(char *str)
{
}

既然你真的触动。

如果你想要理解C,并在没有帮助的情况下做家庭工作,那么就拿到K&R。 立即!

总的来说,您的职能应当用<条码>小范围<>/代码>分配必要的大小。 例如,这里的职能重复:

def repeat_each(char* s) {
  char* res = malloc(strlen(s) * 2 + 1);
  if (!res) return res;
  char* resp = res;
  while (*s) {
    *resp++ = *s;
    *resp++ = *s;
    s++;
  }
  return res;
}

It s the callers responsibility to free the returned string. Pay attention to not call strlen in a loop. If you need it more than once, store it in a temporary variable of type size_t.

如果我理解你想要的话,那就象这样:

void foo(char *str /* , int n */) {
    // do something
    // str[3] =  c ;
}

认为你可以超过为这种扼杀而分配的空间。 因此,可以选择规定期限。

如果你想要改变这种扼杀,但仍保留在原来的储存中。

如果你想根据现有的办法进行新的扼杀,那么:

char* foo(char *str /* , int n */) {
    char *ret = malloc(/* what you need */);
    // do something, probably copy str to ret
    return ret;
}

I think the answer to your question is that you need a fundamental understanding of how strings are represented in C. Once you got that, the processing shouldn t be that difficult anymore.

我建议阅读

rel=“nofollow” http://cplus.about.com/od/ Learningc/s/strings.htm

(我可以看看看我是否合理的话)

Good luck.

在C,你可能还需要通过你在扼杀中使用的最大规模。

char *fill_with_dots(char *data, size_t len) {
    char *dst = data;
    while (--len) *dst++ =  . ;
    *dst = 0;
    return data;
}

Well, it s hard to understand what you ask as "manipulating" can mean a lot.
If you don t want to keep the original then you could define the function as following:

void changeString(char* str);

如果你需要保留原件,那么你可能希望将内容复制到临时缓冲,修改内容,并将之退回:

char* returnModified(char* str )
{
      char* tmp = malloc(strlen(str));
      strcpy(tmp, str);
      // do watever
      return tmp;
}

最新做法确保你适当跟踪所分配的记忆





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签