当前位置: 首页 > 知识库问答 >
问题:

多行字符串输入

丰佐
2023-03-14
int main() {

char userInput[100];    //Store user input

//Take user input
//scanf(" %s",&userInput);
//scanf("%[^\n]s",&userInput);
//scanf("%[^\n]", &userInput);
//gets(userInput);
scanf("%[]s", &userInput); //This takes input but doesnt leave input loop

printf(" %s",userInput);

//i = index to start for looping through the string, starting at the beginning
//count = Stores occurrences of '$'
//inputLength = length of the input, used for limit of loop
int i =0,count =0;
int inputLength = strlen(userInput);

//Loop through the user input, if the character is '$', the integer count will be incremented
for (i; i < inputLength; i++){
    if (userInput[i] == '$'){
        count++;
    }
}

printf("%d", count);
return 0;
}

嗨,我的代码有一些问题,我需要接受一个3行的输入,并计算输入中“$”的数量。输入方法未注释为“scanf(”%[]s“,&userinput);”是唯一一个我发现采取所有3行输入,但我不能打破输入循环继续我的程序。

任何帮助都将不胜感激

共有1个答案

谭安翔
2023-03-14
int readMatrix() {

char userInput[100][3];    //Store user input
int j = 0, m = 0;

for(m = 0; m < 3; m++){
    scanf("%s", &userInput[j][m]); //This takes input (Ex: 22 *(enter)* 33$ *(enter)* 66$ *(enter)*
    j++; //increase the column
}



int i =0,count =0;
m = 0;
//Loop through the user input, if the character is '$', the integer count will be incremented
for (i = 0; i < 100; i++){
    for(m = 0; m < 3; m++){
        if (userInput[i][m] == '$'){
            count++;
        }
    }
}
printf("%d", count);
return 0;
}
 类似资料:
  • 本文向大家介绍groovy 多行字符串,包括了groovy 多行字符串的使用技巧和注意事项,需要的朋友参考一下 示例            

  • 本文向大家介绍groovy 多行字符串(多余的换行符),包括了groovy 多行字符串(多余的换行符)的使用技巧和注意事项,需要的朋友参考一下 示例            

  • 问题内容: 考虑: 这将回显 vv 。为什么以及如何为诸如SimpleXML等之类的东西做多行字符串? 问题答案: 好, 作品。 您还可以使用以下内容: 要么 根据评论进行编辑: 您可以使用运算符来连接字符串。

  • 如果在antlr4 lexer中有一个ONELINE_STRING片段规则标识一行上的一个简单引号字符串,那么如何在lexer中创建一个更通用的字符串规则,该规则将相邻的ONELINE_STRING连接起来(即,只要它们都在不同的行上开始,仅用空格和/或注释分隔)? 即, 将被解析为两个字符串标记,“foo”后跟“bar” 同时: 示例1: Sample3(注意,'output'是该语言中的关键字

  • 主要内容:字符串的输出,字符串的输入其实在《 C语言输入输出》一章中我们已经提到了如何输入输出字符串,但是那个时候我们还没有讲解字符串,大家理解的可能不透彻,所以本节我们有必要再深入和细化一下。 字符串的输出 在C语言中,有两个函数可以在控制台(显示器)上输出字符串,它们分别是: puts():输出字符串并自动换行,该函数只能输出字符串。 printf():通过格式控制符输出字符串,不能自动换行。除了字符串,printf() 还能输

  • 在高版本浏览器和 node.js 中,JavaScript 提供了 ArrayBuffer 和 Uint8Array 等 TypedArray 可以对二进制数据进行操作。但在低版本浏览器中并没有这些对象,而且这些对象没有自动伸缩性,不方便流式操作,相互转换也比较麻烦。 为兼容低版本浏览器,并且可以方便操作字符串和二进制数据,hprose for JavaScript 提供了一个 StringIO