c++ - My program is executing as expected but in the end I am an extra line called a segmentation fault near the output. Could someone see to it? -


am not getting wrong this

#include <cs50.h> #include <stdio.h> #include <ctype.h> int main() {  // prints initials of name     printf("enter name: ");     string s = get_string();       int n= 0;      // prints first letter of name     printf ("%c", toupper(s[0]));       // rememnber index of string starts 0 , counted till \0 , \0 final 1 therefore looks counting 1     // starting @ 0. code accounts      while(s[n] != '\0') // while have not reached end of string     {         while(s[n] != ' ')          {             n = n +1 ;          }      printf("%c", toupper(s[n+1])); // prints letter after space in uppercase order     n = n+1;  // sets n @ letter after space      }     return 0;  } 

input :

rock hilary 

expected output :

rh 

what getting :

rhsegmentation fault 

how fix this?

you've tagged c++ seems you're using entirely c.. i'll answer if it's c.

a segmentation fault occurs when access memory don't own. in case you're trying print character outside of bounds of string. (e.g. string len characters long try print character len+1).

in case happening because of inner while loop. consider string abcd\0.

for outer while loop case s[n] != '\0' succeeds because s[n] 'a'. move inner while loop. letters in string abcd\0 case s[n] != ' ' succeeds because none of characters space. means loop continue search byte evaluates space character past end of string. once find one, if find 1 try print character, inevitably means program segfault because doesn't own memory it's trying access. if program didn't segfault continue this, because algorithm doesn't guarantee you'll ever find stopping point. time if had character , end of string (e.g. a\0 or abc d\0).

you have correct ideas, fix need re-evaluate how want loop execute. outer loop, you're using incrementing variable determine when end loop, consider changing loop:

for (int = 0; s[i] != '\0'; ++i) { } 

this same before, cleaner. real issue inside loop. consider want here. inner loop, you'd need recheck same outer condition. let's try , find solution doesn't that. starting don't want print spaces. simple addition:

for (int = 0; s[i] != '\0'; ++i) {     if (s[i] == ' ')     {         continue;     } } 

using continue ensure we'll restart loop without running of rest of code in loop. we've learend interesting in if: we've print space. can use info if track can print next character (the initial). let's add boolean watch that.

int printedspace = 0; // in c++ `bool printedspace = false;` (int = 0; s[i] != '\0'; ++i) {     if (s[i] == ' ')     {         printedspace = 1; // in c++ `printedspace = true;`         continue;     } } 

now know we're skipping space characters , next iteration we'll remember we've printed space can use info print character. also, back-check our previous condition, we'll stop @ end of string ('\0') read 1 character @ time, , restart loop (continue) if last character in string ' ' (e.g. "abcd \0") won't try execute loop again. anyways, adding print:

int printedspace = 0; // in c++ `bool printedspace = false;` (int = 0; s[i] != '\0'; ++i) {     if (s[i] == ' ')     {         printedspace = 1; // in c++ `printedspace = true;`         continue;     }     else if (printedspace)     {         printf("%c", toupper(s[i])); // note we're printing character , not next 1         printedspace = 0; // in c++ `printedspace = false;`     } } 

two important things notice here: first reset printedspace false don't keep printing character, second if have multiple spaces in row won't break anything, we'll keep executing continue until find non-space character. , frankly that's it. we've guaranteed our count stop @ '\0' , we're printing characters after spaces. bonus, since assume first character in string is initial, if set printedspace true before beginning of loop print first character. means can remove print above loop. remember you're assuming this, break inputting ' rock hilary'. of course you'll print ' rh', since rest of algorithm work intended.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -