如何解决遍历存储在数组中的字符串中的每个字符
我对 C 真的很陌生,而且是在大学的前半年。这是我在 StackOverflow 上的第一个问题。
我的任务是对其进行编程,以便将存储在数字中的每个字符串都转换为十进制,而不会更改主函数之外的任何内容。
我现在试图在过去的 4 个小时里解决这个问题,我想遍历字符串中的每个字符,然后根据那里的位置与长度进行比较,将其转换为十进制。
我在这里唯一的问题是有人帮助我了解如何在不使用 strlen()
的情况下获得字符串长度,因为我无法添加 #include <string.h>
这是我到目前为止得到的(获取数组的长度以遍历每个索引):
#include <stdio.h>
#include <math.h> // Kompilieren mit -lm : gcc -Wall -std=c11 dateiname.c -lm
int main() {
char* numbers[] = {
"01001001","00101010","010100111001","011111110100101010010111","0001010110011010101111101111010101110110","01011100110000001101"};
// Add here..
int length = sizeof(numbers);
for ( int i = 0; i < length; i++ ){
//how do i get the string size without strlen() D:
}
return 0;
}
解决方法
这个项目不需要计算字符串的长度就可以完成。如何?在 C 中,所有字符串都以 nul 结尾,在组成的最后一个字符之后包含 nul-character '\0'
(ASCII 值 0
)字符串。当您需要迭代一个字符串时,您只需循环直到字符值为 0
(例如 nul-character)
这就是所有字符串函数如何知道何时停止读取字符。由于您有一个包含字符串的 指针数组,您只需要遍历每个指针,并为每个指针遍历每个字符,直到找到空字符。
总而言之,(注意你不需要 math.h
),你可以这样做:
#include <stdio.h>
#include <math.h> // Kompilieren mit -lm : gcc -Wall -std=c11 dateiname.c -lm
int main() {
char* numbers[] = {
"01001001","00101010","010100111001","011111110100101010010111","0001010110011010101111101111010101110110","01011100110000001101"};
int nnumbers = sizeof numbers / sizeof *numbers; /* no. of elements */
for (int i = 0; i < nnumbers; i++) {
long long unsigned number = 0;
/* you don't care about the length,strings are nul-terminated,* just loop until \0 is found.
*/
for (int j = 0; numbers[i][j]; j++) {
number <<= 1; /* shift left */
number += numbers[i][j] == '1' ? 1 : 0; /* add bit */
}
printf ("%s = %llu\n",numbers[i],number); /* output result */
}
return 0;
}
(注意:您必须使用 64 位类型来保存转换后的值,因为 "1010110011010101111101111010101110110"
需要最少 38
位来表示)
示例使用/输出
将每个字符串转换为数值的简单示例输出:
$ ./bin/binstr2num
01001001 = 73
00101010 = 42
010100111001 = 1337
011111110100101010010111 = 8342167
0001010110011010101111101111010101110110 = 92790519158
01011100110000001101 = 379917
,
有几种方法可以做到。 IMO,实现 strlen
的一种简单、合理的方法是:
size_t string_length(const char *s) { return strchr(s,'\0') - s; }
但是如果您不被允许使用 strlen
,那么您可能也不允许使用 strchr
。所以你只需要数数。对于一个完整的初学者来说,最惯用的方法可能有点晦涩,所以这里有一个更详细的方法。
请注意,您对数组中元素数的计算无效,已在下方更正。
#include <stdio.h>
int
length(const char *s)
{
int len = 0;
while( *s++ ){
len += 1;
}
return len;
}
int
main(void)
{
char *numbers[] = {
"01001001","01011100110000001101"
};
int count = sizeof numbers / sizeof *numbers; /* Number of entries */
for( int i = 0; i < count; i++ ){
printf(" length of %s is %d\n",length(numbers[i]));
}
return 0;
}
这很主观,但 IMO 更惯用的写法是:
#include <stdio.h>
int
length(const char *e)
{
const char *s = e;
while( *e++ )
;
return e - s - 1;
}
int
main(void)
{
char *numbers[] = {
"01001001","01011100110000001101"
};
char **e = numbers + sizeof numbers / sizeof *numbers;
for( char **t = numbers; t < e; t++ ){
printf(" length of %s is %d\n",*t,length(*t));
}
return 0;
}
,
你可以用这个函数代替strlen()
int strlen_my(char *str){
int len=0;
for(;str[len]!='\0';len++){}
return len;
}
像strlen_my("hello,world!");
一样使用它
#include <stdio.h>
int main(){
char arr[20]="Hello";
int count=0;
while(arr[count]!='\0'){
count++;
}
printf("%d",count);
return 0;
}
看看这个小代码,你就明白了。在 C 中,字符串以 NULL 字符结尾。我们可以利用这一优势。
,在 C 中,字符串实际上只是 char
数组,带有一个特殊的终止符来标记字符串的结尾。所以,假设你有这样的事情:
char *str = "hello";
这基本上等同于:
char str[] = {'h','e','l','o','\0'};
注意到数组末尾的 \0
字符了吗?这是 C 放在字符串末尾的特殊终止符。 strlen()
之类的函数几乎遍历 char
数组,查找第一次出现的 \0
字符,然后停止。
因此,您可以制作自己的 strlen()
版本,像这样说 my_strlen()
:
int my_strlen(char *str)
{
/* Initialize len to 0 */
int len = 0;
/* Iterate through str,increment len,and stop when we reach '\0' */
while(str[len] != '\0')
len++;
/* Return the len */
return len;
}
然后在您的 for
循环中,您可以调用此函数。另外,请注意您对 numbers
数组大小的计算:
int length = sizeof(numbers);
不会给你数组中元素的数量。该代码为您提供大小(以字节为单位)或 numbers
,它是一个 char
指针数组。如果您想获得元素的数量,您必须将该大小除以单个元素(即 char
指针)的大小(以字节为单位)。所以,这样的事情会起作用:
int length = sizeof(numbers) / sizeof(numbers[0]);
您的最终代码可能如下所示:
#include <stdio.h>
#include <math.h> // Kompilieren mit -lm : gcc -Wall -std=c11 dateiname.c -lm
int my_strlen(char *str) {
/* Initialize len to 0 */
int len = 0;
/* Iterate through str,and stop when we reach '\0' */
while(str[len] != '\0')
len++;
/* Return the len */
return len;
}
int main() {
char* numbers[] = {
"01001001","01011100110000001101"};
// Add here..
// Notice the change here
int length = sizeof(numbers) / sizeof(numbers[0]);
for(int i = 0; i < length; i++ ){
int str_len = my_strlen(numbers[i]);
// Do what you need with str_len
}
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。