How to do that? I am willing to add the character -
after every character in a certain string. For a specific case assume I have a string declared in the size of 100 but filled with only 3 letters (entered by the user - so the value of the character str[3]
is NULL (\0
)).
怎么做?我愿意添加字符 - 在某个字符串中的每个字符之后。对于特定情况,假设我有一个声明为100的字符串,但只填充了3个字母(由用户输入 - 因此字符str [3]的值为NULL(\ 0))。
for(i = strlen(str) ; i >= 0 ; i--)
{
str[i*2] = str[i];
}
for(i = 0 ; i < strlen(str) ; i++)
{
if(i % 2 != 0)
{
str[i] = '-';
}
}
but it's not working, help anyone?
但它不起作用,帮助任何人?
4 个解决方案
#1
0
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len = 0;
int i = 0;
strcpy(str, "abcd");
len = strlen(str);
// len = 4
// Expected output: "a-b-c-d"
// str[7] = '\0';
// str[6] = 'd';
for(i = len-1; i >= 0 ; i--)
{
str[i*2+1] = '-';
str[i*2] = str[i];
}
str[len*2-1] = '\0';
printf("%s\n", str);
}
#2
3
You forgot to handle the terminating zero of the string in the first loop. After it, the original terminating zero is still in place:
你忘了在第一个循环中处理字符串的终止零点。之后,原始终止零仍然存在:
A S D \0
A S S \0 D x \0
So at the beginning of the second loop strlen(str)
is still 3.
所以在第二个循环开始时,strlen(str)仍然是3。
When the second loop gets to the first \0
to replace it with -
, the loop terminates instead, due to the condition in the for
statement.
当第二个循环到达第一个\ 0以用 - 替换它时,由于for语句中的条件,循环终止了。
You'd better insert the hyphens straight away in the first loop.
你最好在第一个循环中直接插入连字符。
for (i = strlen(str); i > 0; i--) {
str[i*2] = str[i];
str[i*2-1] = '-';
}
Note, the loop above goes down to 1 only, as you can leave the A
in place anyway.
请注意,上面的循环只会降为1,因为无论如何都可以将A留在原位。
#3
0
You can use a secondary string to store your changed results.
您可以使用辅助字符串来存储更改的结果。
char str[100] = "ASD";
char changed[100*2 - 1] = "";
for(int i = 0, j = 0; i < strlen(str); i++, j+=2)
{
changed[j] = str[i];
if (i < strlen(str)-1)
{
changed[j + 1] = '-';
}
}
for (int i = 0; i < strlen(changed); i++)
{
printf("%c", changed[i]);
}
#4
0
use only first for loop and make this changes to your for loop
仅使用first for循环并对for循环进行此更改
for(i = strlen(str)-1 ; i > 0 ; i--)
{
str[i*2] = str[i];
str[i*2-1] = '-';
}
#1
0
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len = 0;
int i = 0;
strcpy(str, "abcd");
len = strlen(str);
// len = 4
// Expected output: "a-b-c-d"
// str[7] = '\0';
// str[6] = 'd';
for(i = len-1; i >= 0 ; i--)
{
str[i*2+1] = '-';
str[i*2] = str[i];
}
str[len*2-1] = '\0';
printf("%s\n", str);
}
#2
3
You forgot to handle the terminating zero of the string in the first loop. After it, the original terminating zero is still in place:
你忘了在第一个循环中处理字符串的终止零点。之后,原始终止零仍然存在:
A S D \0
A S S \0 D x \0
So at the beginning of the second loop strlen(str)
is still 3.
所以在第二个循环开始时,strlen(str)仍然是3。
When the second loop gets to the first \0
to replace it with -
, the loop terminates instead, due to the condition in the for
statement.
当第二个循环到达第一个\ 0以用 - 替换它时,由于for语句中的条件,循环终止了。
You'd better insert the hyphens straight away in the first loop.
你最好在第一个循环中直接插入连字符。
for (i = strlen(str); i > 0; i--) {
str[i*2] = str[i];
str[i*2-1] = '-';
}
Note, the loop above goes down to 1 only, as you can leave the A
in place anyway.
请注意,上面的循环只会降为1,因为无论如何都可以将A留在原位。
#3
0
You can use a secondary string to store your changed results.
您可以使用辅助字符串来存储更改的结果。
char str[100] = "ASD";
char changed[100*2 - 1] = "";
for(int i = 0, j = 0; i < strlen(str); i++, j+=2)
{
changed[j] = str[i];
if (i < strlen(str)-1)
{
changed[j + 1] = '-';
}
}
for (int i = 0; i < strlen(changed); i++)
{
printf("%c", changed[i]);
}
#4
0
use only first for loop and make this changes to your for loop
仅使用first for循环并对for循环进行此更改
for(i = strlen(str)-1 ; i > 0 ; i--)
{
str[i*2] = str[i];
str[i*2-1] = '-';
}