Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop stopping halfway ansi C
    text
    copied!<p>I have the following code:</p> <pre><code>void toCapital(char name[], int size){ int i = 0; char *wholeName = name; for (i = 0; i &lt; size ; i++){ wholeName[i] = toupper(wholeName[i]); printf("%c", wholeName[i]); } } main() { char miNombre[] = "Jason Martin Marx"; toCapital(miNombre, sizeof(miNombre)); } </code></pre> <p>And the output is:</p> <pre><code>JASON MA </code></pre> <p>This code takes a char array and converts all the strings inside into upper case. However, for some reason it stops halfway. Even if i increase the number of times to run the loop, it just adds gibberish at the end instead of the following letter.</p> <p>If i was to edit the array into something like "Jason Martin Marx Jason Martin Marx" (doubling the string size) then it would print out the upper cased string once as "JASON MARTIN MARX" but not the second time.</p> <p>Here is the whole code as requested: </p> <pre><code>#include&lt;stdio.h&gt; #include&lt;string.h&gt; #include &lt;stdlib.h&gt; void myName(char name[], int size){ int i; for (i = 0; i &lt; size -1; i++){ char currentLetter = name[i]; if (currentLetter == 'a' || currentLetter == 'e' || currentLetter == 'i' || currentLetter == 'o' || currentLetter == 'u' || currentLetter == 'A' || currentLetter == 'E' || currentLetter == 'I' || currentLetter == 'O' || currentLetter == 'U'){ printf("Character [%c] located at position %i is a vowel\n", currentLetter, i); } else if (currentLetter == ' '){ printf("Character [%c] located at position %i is a space\n", currentLetter, i); } else if (currentLetter == '$' || currentLetter == '%'){ printf("Character [%c] located at position %i is a symbol\n", currentLetter, i); } else{ printf("Character [%c] located at position %i is a consonant\n", currentLetter, i); } } } void pyramidA(char name[], int size){ int i; char *wholeName = name; int pointer = size-1; char spaces[80] = ""; for (i = 0; i &lt; (size / 2) ; i++){ printf("%i %s [%s] \n", pointer, spaces, wholeName); wholeName++; ///erases first letter wholeName[strlen(wholeName) - 1] = '\0'; /// erases last letter pointer = pointer - 2; strcat(spaces," "); } } void toUpper(char name[], int size){ int i = 0; char *wholeName = name; printf("%s", wholeName); for (i = 0; i &lt; size ; i++){ wholeName[i] = toupper(wholeName[i]); printf("%c", wholeName[i]); } } main() { char miNombre[] = "Jason $ Martin % Marx "; myName(miNombre, sizeof(miNombre)); printf("\n"); pyramidA(miNombre, sizeof(miNombre)); printf("\n"); toUpper(miNombre, sizeof(miNombre)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload