gpt4 book ai didi

c - 计算所选单词的字符数的程序

转载 作者:行者123 更新时间:2023-11-30 18:03:58 25 4
gpt4 key购买 nike

这是一项家庭作业,对此感到抱歉。我无法制作用于计算单词中字符的工作程序,例如:

  • 我输入字符串:我的名字是 Peter
  • 程序询问要处理哪个单词..
  • 我输入数字:3
  • 程序显示:第三个单词的计数为 2。
<小时/>
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char text[200],vards[20];
int i, length,lengthv, count=0,x;
printf("insert txt\n");
gets(text);
length=strlen(text);
for(i=0; i<length; i++)
{
if(text[i]!=' ' && text[i]!='.' && text[i]!=',')
{
printf("%c", text[i]);
if (text[i+1]=='\0')
count++;
}
else
{
if(text[i-1]!=' ' && text[i-1]!='.' && text[i-1]!=',')
{
count++;
printf("\n");
}
}
}
printf("detect lenght of wich name\n");
for(i=0;i<x;i++);
scanf("%s", &text);
lengthv=strlen(vards);
printf("\n The lenght of name is %d", lengthv);
getch();
return 0;
}

最佳答案

我不太明白你的代码,但我会这样做:

#include <stdlib.h>
#include <stdio.h>

int main() {
char text[200], whichText[200];
int i=0, length, countWord=0, currWord=1, wordChars=0;

// Get text input:
printf("insert txt\n");
gets(text);
length=strlen(text);

// Get word to count:
while(countWord == 0) {
printf("Count which word?\n");
gets(whichText);
sscanf(whichText, "%i", &countWord);
}

// Iterate through each character of the text input:
for( i=0; i < length; i++ ) {
// Keep track of which word we are on, by counting spaces:
if( text[i] == ' ' ) {
currWord ++;
continue;
}
// While we are on the desired word, count the characters:
if( currWord == countWord )
wordChars ++;
}

printf("Count of word %i is %i.\n", countWord, wordChars);
return 0;
}

关于c - 计算所选单词的字符数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7890441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com