c - Why my program outputs weird characteres? -


when use 3 or more names programs outputs weird characters.

when use 2 totally fine, when use lot of spaces well.

but when try kaue rodrigo pacheco output is: krp]a

#include <stdio.h> #include <cs50.h> #include <string.h>  int main(void) { string name = get_string("what name?\n");  // initiate array contain initials char initials[10];  // initials index int index = 0;  // if first character not space append initials if (name[0] != 32) {         // if character not between a-z         if (!(name[0] >= 'a' && name[0] <= 'z'))         {             // transform in uppercase             name[0] = name[0] - 32;         }         // append character initials array         initials[index] = name[0];         // keep track on how many done         index++; }   // iterate through user input (int = 1; < strlen(name); i++)  {     if ((name[i] != 32) && (name[i - 1] == 32))     {         // if character after blankspace not between a-z         if (!(name[i] >= 'a' && name[i] <= 'z'))         {             // transform in uppercase             name[i] = name[i] - 32;         }         // append character initials array         initials[index] = name[i];         // keep track on how many done         index++;     } } // end result, prints initials uppercase printf("%s\n", initials); 

}

add

initials[index] = 0; before printf. not terminate char array zero. c string number of chars 0 @ end


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -