小编zoi*_*ds3的帖子

切换字母(没有字符串或字符串函数)

我想在每次出现在文本中时将"cat"这个词切换成"dog".我不能使用字符串或字符串函数.

我的代码:

#include <stdio.h>

int main()
{   
    int i; // loop counter 
    int size; // size of arry  
    int input[20];
    printf("enter text here\n");  

    while((input[i] = getchar()) != '\n') // input text to the arry
    {
        if(input[i]=='c' && input[i+1]=='a' && input[i+2]=='t') // switching characters
        {
            input[i]='d'; input[i+1]='o'; input[i+2]='g'; 
        }

        i++; 
        size++;  
    }

    i=0; // reset for next loop

    while(i <= size) // printing the text out ofthe arry 
    {
         putchar(input[i]); 
         i++;
    }

    printf("\n"); 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

enter text here                                                                                                                                  
cat …
Run Code Online (Sandbox Code Playgroud)

c arrays text

0
推荐指数
1
解决办法
127
查看次数

标签 统计

arrays ×1

c ×1

text ×1