Originally Posted by
camel-man
then default should be S[i] = O[i];
Originally Posted by
Gkendoyan
Ok i fixed that, thank you! But now what's happening is that its only reading the first character. and if the first character is a lower case alphabet, its transfering to upper case printing that. if i put in a number, its printing back the first number.
Where's the mistake?
The mistake is that you picked horrible variable names. They don't describe anything. Honestly, I would have guessed O is your "output" array, and S is your "string" or "alphabet" array, so I didn't notice that mistake right away. If, instead you had array names like original and converted, not only would it make your code more clear, it would be much easier to spot the mistake you made of reversing the assignment -- in fact, with clean, well formatted code and descriptive names, it's much less likely you'll even even make mistakes in the first place.
As for the double increment problem, it would be good if you you started learning to use a debugger. A debugger allows you to step through your program line by line and examine all the variables at each moment. IT's one of the most important tools for a programmer. For smaller programs, sometimes you can just add a few printf statements to see what is going on, and remove them when you have the problem(s) fixed. Putting the following at the end of the for loop would have caught the problem:
Code:
printf("Converting %c in input, to %c in array position %d\n", O[i], S[i], i);
Also, you have a potential problem with your input. You don't null terminate it, so it's not a proper C-string. Your code assumes that the user always enters N characters, but it will break if anybody enters less than N (by converting and printing garbage characters at the end of the input array). You have two options:
1. Store the length in a separate variable, and use that to limit your for loop instead of N.
2. Null-terminate the input array. This has the advantage of making it easier to work with and print later, as there are a lot of standard functions for working with strings. You can use strlen to get it's length at any time, and you can still use a variable to track the length so you don't have to call strlen every time. Note that you have to save one spot in the array for the null terminator, so your input loop would be
Code:
while ( (ch=toupper(getchar()) ) != '\n' && ch != EOF && i<N-1) {
original[i++] = ch;
}
original[i] = '\0'; // add null terminator
original_length = i; // store the length in a variable for easy use later
...
// easy print:
printf("The original number is: %s\n", original); // no looping through char by char
A few things to note about the above
1. Check for EOF as well as '\n'. I may redirect input from a file or pipe it from another program, which may send EOF to signal end of input. You should always handle EOF when dealing with input.
2. i < N-1 to save space for the null terminator. Define N as 16 if you want room for 15 characters of input + 1 for the null terminator.
3. You can combine the increment and assignment by putting i++ in the array index.
4. If you similarly put a null terminator at the end of the converted string, you can use the same techniques and standard functions.