#include<stdio.h>
#include<stdlib.h>
char *syllable[26] = {"a","bub","cash","dud","e","fud","gug","hash","i","jay",
"kuck","lul","mum","nun","o","pub","quack","rug","sus",
"tut","u","vuv","wack","xux","yuck","zug"};
void Tutnese(char *word, char *newword);
char *letter;
void Tutnese(char *word, char *newword)
{
//clrscr();
for(*letter = 'A'; *letter <= 'Z'; *letter++)
{
letter=syllable;
printf("%c\n",&letter);
}
}图尼语是一种英语游戏,主要由儿童使用,他们用它与成年人交流(或相反)。
我正试着让A="A“、”B=“、bub”c=“现金等等。我期待着这样的结果。
“电脑”变成“卡西姆普布图特鲁格”--“石头”变成了“苏斯特通纽克”
但我刚开始学习c,我不知道如何使用指针。我一直在犯错误,就像赋值一样,在没有强制转换的情况下从指针中生成整数
发布于 2013-10-14 05:03:29
代码
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *syllable[26] = {"a","bub","cash","dud","e","fud","gug","hash","i","jay",
"kuck","lul","mum","nun","o","pub","quack","rug","sus",
"tut","u","vuv","wack","xux","yuck","zug"};
void Tutnese(char *word, char *newword, size_t new_size);
void Tutnese(char *word, char *newword, size_t new_size)
{
char *end = newword + new_size;
char c;
while ((c = *word++) != '\0')
{
if (!isalpha(c))
*newword++ = c;
else
{
char *tut = syllable[tolower(c) - 'a'];
ptrdiff_t len = strlen(tut);
if (end - newword <= len)
break;
memcpy(newword, tut, len + 1);
newword += len;
}
}
*newword = '\0';
}
int main(void)
{
char i_data[1024];
char o_data[4096];
while (fgets(i_data, sizeof(i_data), stdin) != 0)
{
Tutnese(i_data, o_data, sizeof(o_data));
printf("I: %sO: %s", i_data, o_data);
}
return(0);
}输出
I: computer
O: cashomumpubututerug
I: how do you tell mum that she cannot understand us?
O: hashowack dudo yuckou tutelullul mumumum tuthashatut sushashe cashanunnunotut ununduderugsustutanundud usus?
I: The quick brown fox jumped over the lazy dog.
O: tuthashe quackuicashkuck bubrugowacknun fudoxux jayumumpubedud ovuverug tuthashe lulazugyuck dudogug.发布于 2013-10-14 04:23:27
char *letter;
该语句声明一个名为letter的变量,与其他任何语句(如char ch; )一样。
那又有什么区别呢!!
区别(和相似之处)是:
char ch;声明一个char变量,即分配一个大小为1字节的内存块(静态地),您可以使用ch引用它。char char *letter;声明一个char pointer,即内存大小为2或4或8个字节(取决于编译器)将被分配(同样是静态的)来存储变量的地址。现在,当您像在*letter循环中一样使用for作为lvalue (左侧)时,这意味着您试图写入存储在letter中的内存地址。在您的示例中,您从未在letter中存储任何地址,因此您可以使用letter = &ch;,其中ch是一些char变量。
就这么说教了!!
现在我对你们节目的建议是:
letter指针,一个简单的char i变量就可以了。syllable[orig_string[i] - 'A'],在for循环中一直连接到orig_string的末尾。假设orig_string包含所有大写字母表printf语法。一定要从好的源代码中阅读C中的指针,因为它们永远不会离开您,并且会给您带来各种各样的噩梦。
发布于 2013-10-14 03:49:44
让我们忘记指针,分解问题。您将得到一个单词word,您希望根据映射创建newword。
首先,您需要知道newword有多大。为此,迭代word中的字符并添加映射的字符串长度(称为N),这样就可以为newword (通过malloc)分配N+1字节(字符串在C中为null )。然后,您再次遍历这些字符,并附加到newword中。
让我给您一些提示:要迭代一个字符串(让我们称它为word),C代码如下所示:
unsigned int wordlen = strlen(word);
for(unsigned int index = 0; index < wordlen; index++)
printf("Character at %u is %c", index, word[index]);你的for循环搞砸了。请在C中查阅一些关于指针和字符串操作的教程。
https://stackoverflow.com/questions/19353199
复制相似问题