-2

I'm trying to do a program that receives an array of characters as input and each letter is replaced by another. On the following code, I only consider one change ('a'-->'h'), but as soon as the program works for that change, I will implement the others (based, for example in Caesar's Cipher). The thing is that the program compiles successfully, however, when I run it, if I insert an 'a', he return another 'a' (and it was supposed to return an 'h'). What is wrong with the code? Thanks in advance.

#include <stdio.h>

void ciphering (char text[])
{
    int i;

    for (i=0; i!='\0'; i++)
    {
         if (text[i]>='a' && text[i]<='z')
         {
                  if (text[i]=='a')
                      text[i]='h';
         }
    }
}

int main()
{
    char text[1000];

    scanf("%s", text);
    ciphering(text);
    printf("%s\n", text);
    return 0;
}
John Odom
  • 1,189
  • 2
  • 20
  • 35
cooper
  • 23
  • 3
  • This line `for (i=0; i!='\0'` is incorrect. `'\0'` is `0`. It should be `for (i=0; text[i]!='\0'`. – Evil Dog Pie Mar 23 '15 at 18:23
  • 1
    Instead of `scanf("%s", text);`, you should use `fgets( text, sizeof ( text ), stdin );` for safety... it guarantees no more than sizeof text is ever read, preventing potential memory errors.... it is a good habit to get into, errors like that are what hacker exploits are made from. – JohnH Mar 23 '15 at 18:32
  • @JohnH Thank you for your suggestion. It actually solved another problem on my code (program was stopping when whitespace's were inserted). – cooper Mar 24 '15 at 17:39

3 Answers3

1

Your for loop in the function has

for(i = 0; i != '\0'; ++i)
           ^^^^^^^^^

Change it to:

for(i = 0; text[i] != '\0'; ++i)

because you want to check when the text[i] has a NUL character.

shauryachats
  • 9,975
  • 4
  • 35
  • 48
1

0 and '\0', in the context of your for loop (and many other contexts) are the same.

for(i = 0; i != '\0'; ++i)

I read that as: "for i from zero while it's different than zero ..." which effectively means the loop will not run even a single time.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • Apparently, the loop will run *infinitely*. – shauryachats Mar 23 '15 at 18:28
  • 1
    @volerag: it will not run. Not once, let alone an infinite number of times. – pmg Mar 23 '15 at 18:31
  • I think `i` would never be `\0` which makes `i != '\0'` true, hence infinite? – shauryachats Mar 23 '15 at 18:32
  • The assignment `i = 0` makes it zero. Then the comparison `i != '\0'` yields false ... the increment is never executed – pmg Mar 23 '15 at 18:34
  • I may be wrong, but `0 != '\0'` is true, right? *Zero is not equal to null character? True.* – shauryachats Mar 23 '15 at 18:36
  • Ah! I see your problem now. But `0` and `'\0'` are absolutely identical (except in source form): the first is an integer literal with type `int` and value `0`; the second is a character literal with type `int` and value `0`. – pmg Mar 23 '15 at 18:39
  • Oh! I am so sorry, I didnt know even a basic difference in C. @pmg I'm really sorry. Thanks a lot for taking the time to convince me. :) – shauryachats Mar 23 '15 at 18:43
0

The program does not enter into the for() loop because the continuation condition is false:

for (i = 0; i != '\0'; i ++) {

What you need to test is that i (the index of the character currently analyzed) does not point to the null character. That line should read:

for (i = 0; text[i] != '\0'; i ++) {
axiac
  • 68,258
  • 9
  • 99
  • 134