c ++问题给定一个字符串数组,输出总数

时间:2018-11-09 14:34:08

标签: c++

给出一个字符串数组,输出列表中的字符总数(n)。例如:a = [“ Dance”,“ with”,“ me”] => n = 10;对于a = [](空数组,没有元素),n = 0。

我有以下代码,也可以在这里找到:http://cpp.sh/8mtq4

#include<stdio.h>

int main(void){
    size_t size, i=0;
    int count=0;
    char* names[]={"Dance", "With", "Me"};

    size = sizeof(names)/sizeof(char*);

    for(i=0;i<size;i++){
        printf("%d - %s\n",count+1, *(names+i));
        count++;
    }

    printf("\n");
    printf("The number of strings found are %d\n",count);
    return 0;
}

下一步的想法?我没找到...

2 个答案:

答案 0 :(得分:1)

添加变量:

@echo off
set "psCommand="(new-object -COM 'Shell.Application').BrowseForFolder(0,'Please choose your SOURCE folder.',0x270,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "location=%%I
echo %location%
pause
exit

在forloop中,

int totalch = 0;

然后最后打印总计

totalch += strlen(names[i]);

答案 1 :(得分:-2)


>using namespace std; 
>int sum=0 //sum of our chars
>string s[]={"Dance","With","Me"};`
>for(int i=0;i<sizeof(s)/sizeof(s[0]);i++)//sizeof(s)/sizeof(s[0]) gives number of elements in array
>sum+=s[i].length(); //our sum is sum+ length of specify string in array