Lauren Morgan
Lauren Morgan

Reputation: 33

Code outputs random symbols, i'm unsure what is wrong

I've made a program that shortens full names to initials, and removes any spaces between what has been entered. It did work before but now it prints the initials but also random symbols? I can't really figure out why it's doing it. I'm new to programming also.

This is my code:

 // This code removes the spaces from the inputted name 

char *removeSpaces(char *str) 
{ 
    int i = 0, j = 0; 
    while (str[i]) 
    { 
        if (str[i] != ' ') 
           str[j++] = str[i]; 
        i++; 
    } 
    str[j] = '\0'; 
    return str; 
} 

// This code takes the users name, and shortens (sh) it

int main(void) {

    char str[100],sh[20];
    int j=0;

    cout<<"Enter Full Name :";
    cin.getline(str,30);

    for(int i=0;i<strlen(str);i++)
      {
       if(i==0){
         sh[j]=str[i];
         sh[++j]=' ';
        }

       else if(str[i]==' '){
         sh[++j]=str[i+1];
         sh[++j]=' ';
        }
       }

// This then takes the remove spaces code, and prints the initials with a new line

    cout << removeSpaces(sh) <<endl;
    cout << "\n" <<endl;

   return 0;
}

Picture of the output

Upvotes: 3

Views: 74

Answers (3)

kadina
kadina

Reputation: 5382

You are missing adding string terminator character ('\0') to string sh. Below is the program.

#include <stdio.h>

char *removeSpaces(char *str) 
{ 
    int i = 0, j = 0; 
    while (str[i]) 
    { 
        if (str[i] != ' ') 
           str[j++] = str[i]; 
        i++; 
    } 
    str[j] = '\0'; 
    return str; 
} 

// This code takes the users name, and shortens (sh) it

int main(void) {

    char str[100],sh[100];
    int j=0;

    cout<<"Enter Full Name :";
    cin.getline(str,30);

    for(int i=0;i<strlen(str);i++)
      {
       if(i==0){
         sh[j]=str[i];
         sh[++j]=' ';
        }

       else if(str[i]==' '){
         sh[++j]=str[i+1];
         sh[++j]=' ';
        }
       }

       sh[j+1] = '\0';

// This then takes the remove spaces code, and prints the initials with a new line

    cout << removeSpaces(sh) <<endl;
    cout << "\n" <<endl;

   return 0;
}

Enter Full Name :ra me ge rmg

Upvotes: 1

user10957435
user10957435

Reputation:

You don't terminate sh with \0 after you're done, yet removeSpaces() expects a null character at the end of the string. Because of this, removeSpaces() could go past your intended boundary.

Just add this line after your for in main():

sh[++j] = '\0\;

Word of warning: You should always make sure that j is < 20 (the size of sh) before you set it. Otherwise, you could go past the boundary of sh. That could also become a source of problems.

Upvotes: 0

Adrian Mole
Adrian Mole

Reputation: 51864

You have missed out a line (I guess) after the for loop in your main function, which means your string is potentially not null-terminated.

Using the same (correct) logic you have in your removeSpaces function, just add this line immediately after the for loop in main:

sh[++j] = '\0';

Upvotes: 1

Related Questions