我正在尝试编写一个程序来计算给定字符串的每个字母中的字母,并将计数附加到该字词上。如何在C中将字符数组重置为0/NULL?
示例 - I/P:蛋糕O/P:The3 cake4
以我的逻辑,我想重置命名的类型整数(完成)&的icnt
计数器变量和命名的temp
阵列键入字符(不工作)。 我做了一些关于StackOverflow的研究,并实现了memset
来重置,但看起来它不适合我!
如果有错误,请更正我的程序。
#include<stdio.h>
#include<string.h>
void fun(char*,char*);
char ch[50];
char temp[10]={0};
int main()
{
printf("Enter string :");
scanf("%[^'\n']s",&ch);
fun(ch,temp);
}
void fun(char *ptr,char *tptr)
{
int icnt = 0;
while(*ptr!='\0')
{
if(*ptr!=' ')
{
*tptr = *ptr;
tptr++;
icnt++;
}
else if(*ptr==' ')
{
printf("In IF bcoz of space\n");
printf("%s %d\n",temp,icnt);
icnt = 0;
//temp[10] = {0}; //*
memset(temp,0,10); //*
//Tried reseting in both the above ways *
}
ptr++;
}
if(*ptr=='\0')
{
printf("%s%d\n",temp,icnt);
}
}
输出上述程序的:The34
看起来像什么也不得到存储在所述第二时间在临时变量
的例外输出:The3 cake4
fun函数将找到的单词写入'* tptr'中,而不是直接写入'temp'变量。所以一旦你发现一个词,你应该'tptr'重新初始化为'&temp'。 –