【发布时间】:2012-10-05 00:16:01
【问题描述】:
好的,长话短说我遇到了一些麻烦: 我正在尝试将字符数组(字符串)的起始地址发送到函数。一旦函数获得指针,我希望函数一一解析字符(以修改某些字符)。截至目前,我只是将其设置为打印每个字符,但我仍然非常失败。
这就是我所拥有的:
#include "system.h"
#include <stdio.h>
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
void EXCLAIM(char *msg){
char the_char = 0;
the_char = msg;
while (the_char != 0)
{
printf(the_char);
*msg = *(msg++);
the_char = *msg;
}
}
int main(void) {
char *first_str = "This is a test. Will this work. I. am. Not. Sure...";
while (1) {
EXCLAIM(first_str);
}
}
编辑:
这是我尝试做的更新后的代码;发送指针并遍历每个字符,用感叹号替换所有句点。
#include "system.h"
#include <stdio.h>
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
void exclaim(char *msg){
int i;
for( i=0; msg[i]; i++ )
{
if (msg[i] == '.') {
msg[i] = '!';
}
}
printf(msg);
}
int main(void) {
char *the_sentences = "This is a test. Will this work. I. am. Not. Sure...";
while (1) {
exclaim(the_sentences);
}
}
感谢大家的帮助!
【问题讨论】:
标签: c arrays string pointers character