简体   繁体   English

(适用于初学者):输入字符数组,无特定大小。 使用:字符数组。 创建一个实现间隔的条件。 (在C中)

[英](For beginners): Input Array of Characters w/ no specific size. Use of: Array of Characters. Create a condition that implements an interval. (in C)

Function that, given a person's name, prints his initials. 给定一个人的名字的函数,打印其首字母缩写。

1) Can someone please show how to input an array of characters, making the array as long as the user types characters. 1)有人可以显示如何输入字符数组吗,只要用户键入字符,就可以使该数组成为可能。 (LIKE step by step, its super complicated for a beginner, and explain the functions you use) <3 <3 (喜欢一步一步,对于初学者来说超级复杂,并解释您使用的功能)<3 <3

2) Then how to USE that array of characters in a function, loop or to print a character of that string at a certain index. 2)然后如何在函数中使用该字符数组,循环或在某个索引处打印该字符串的字符。

3) Also can someone please demonstrate how you would make a condition that has an interval as per the below in a loop, ex: 3)还可以请某人演示如何处理循环中具有以下间隔的条件,例如:

variable < [65-90] <---- how would you formally type it in code 变量<[65-90] <----您将如何在代码中正式键入它

4) Any advice on where to improve the function ? 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

}

First off, try to read a basic ansi C book first, this is all basic stuff. 首先,尝试首先阅读基本的ansi C书籍,这些都是基本知识。 You sample code looks really weird. 您的示例代码看起来真的很奇怪。 C doesn't have 'Strings', it was only arrays, that being said, if your goal is to get initials, a code like the following will make it. 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); 
}
  1. If an array is array[100], you can just walk the array by using an Int variable inside as array[a], where a is 'Int a=0;, then increase a in the function (a++) or point to an specific location, let's say, array[50]. 如果数组是array [100],则可以通过使用内部的Int变量作为array [a]来遍历数组,其中a为'Int a = 0 ;,然后在函数(a ++)中增加a或指向a比方说,特定位置array [50]。

  2. variable < [65-90]: If used in a while, you can make something like this. 变量<[65-90]:如果一段时间使用,您可以进行如下操作。

    while (a>=65 && a<=90) { some code here } 而(a> = 65 && a <= 90){这里有一些代码}

That will limit the actions only when a is between your range. 仅当a在您的范围内时,这将限制操作。

  1. I just gave my advice, strings don't exist! 我只是给出了建议,字符串不存在! they are just arrays, so... to do things like toupper you have to use a function that will walk your array, converting 1 by 1, you can do this with pointers so you just call the reference and has lower overhead, even though, this program is so little so, don't worry about it. 它们只是数组,所以...要做类似的事情,您必须使用将遍历数组,将1转换为1的函数,您可以使用指针执行此操作,因此即使调用引用也具有较低的开销,即使,这个程序太少了,不用担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM