gpt4 book ai didi

c - (对于初学者) : Input Array of Characters w/no specific size. 使用 : Array of Characters. 创建一个实现间隔的条件。 (在C中)

转载 作者:行者123 更新时间:2023-11-30 16:38:22 26 4
gpt4 key购买 nike

在给定一个人的名字的情况下打印他的姓名缩写的功能。

1)有人可以演示如何输入字符数组,只要用户输入字符就可以生成数组。 (LIKE一步一步,对于初学者来说 super 复杂,并解释一下你使用的功能)<3 <3

2) 然后如何在函数、循环中使用该字符数组或在某个索引处打印该字符串的字符。

3)有人可以演示如何在循环中创建一个具有间隔的条件,例如:

变量 < [65-90] <---- 你会如何在代码中正式键入它

4)对于改进功能有什么建议吗?

<小时/>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int giveInitials(void)
{

char name[], upperCasedName[];

name = fgets("Full Name: ");
uppperCasedName = toupper(name);


int n, count, i;

n = strlen(name);
count = 0;
i = count;


while (count < n)
{
// While Loop1: iterates until (int) value at index of char array (upperCasedName) = 65-90 (until it gets to the first letter)
while ((int)upperCasedName[count] != [65-90] )
{
for (i = count; i < n ; i++)
{
count++;
}
}

printf("%c", upperCasedName[count]);

// While Loop2: iterates until (int) value at index of char array (upperCasedName) != 65-90 (until it gets to a none-letter)
while ((int)upperCasedName[count] = [65-90])
{
for (i = count; i < n; i++)
{
count++;
}
}

} // End of While loop

}

最佳答案

首先,尝试先阅读一本基础的ansi C 书,这些都是基础的东西。你的示例代码看起来很奇怪。 C 没有“字符串”,它只是数组,也就是说,如果您的目标是获取缩写,则像下面这样的代码就可以实现。

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

int main (int argc, char * argv[])
{

char c;
char initials[1];
int flag1=0;
int counter=0;

while ( (c=getchar()) != EOF )
{
if (counter==0){
initials[0]=c;
printf ("\nFirst char is %c\n", c);
}

if (flag1==1){initials[1]=c;break;}
if ( c == ' ')
{
flag1=1;
}
counter++;

}
printf ("\nInitials: %s\n", initials);
}
  • 如果数组是 array[100],则可以通过在数组[a]中使用 Int 变量来遍历数组,其中 a 是 'Int a=0;,然后在函数中增加 a ( a++) 或指向特定位置,例如 array[50]。

  • 变量<[65-90]:如果使用一段时间,你可以做出这样的东西。

    同时 (a>=65 && a<=90){这里有一些代码}

  • 仅当 a 位于您的范围内时,才会限制操作。

  • 我只是给出了我的建议,字符串不存在!它们只是数组,所以...要做像 toupper 这样的事情,你必须使用一个函数来遍历你的数组,将 1 转换为 1,你可以使用指针来完成此操作,这样你只需调用引用并且开销较低,即使,这个程序很小,不用担心。
  • 关于c - (对于初学者) : Input Array of Characters w/no specific size. 使用 : Array of Characters. 创建一个实现间隔的条件。 (在C中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47517770/

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