Thread: wgiven a person’s name, prints a person’s initials

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    5

    wgiven a person’s name, prints a person’s initials

    Hi there! I'm having troubles with my code. I am trying to write up a program that prints out a person's initials, given a person's name. Nothing appears after running the code and inputting anything.


    Code:
    //Implement a program that, given a person’s name, prints a person’s initials.
    
    
    #include <stdio.h>
    #include <cs50.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main(int argc, string argv[])
    {
        // Prompt the user to type their full name.
        printf("What is your full name? ");
        string s = get_string();
        
        // Checks if the string is not NULL.
        if (s != NULL)
        
        {   
            // Iterate over strings in argv. 
            // i = 1 because we don't want the program name to be included.
            for(int i = 1;  i < argc; i++ )
                {
                    // Prints the first letter of each string in the array.
                    printf("%c", toupper(argv[i][0]));
                }
                
            printf("\n");
        }
        
    
    
    }
    Any ideas?
    Last edited by Jason Dang; 02-26-2017 at 10:03 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > string s = get_string();
    ...
    > printf("%c", toupper(argv[i][0]));
    Why are you looking at argv, when the input is apparently in s ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    5
    Oh totally sorry about that. I didn't even need to be using that anyway. I rewrote the program from scratch. Here is the new code.

    Code:
    #include <stdio.h>
    #include <cs50.h>
    #include <string.h>
    #include <ctype.h>
    
    
    int main(void)
    {
        // Prompt the user to type their full name.
        printf("What is your full name? ");
        string s = get_string();
        
        // Checks if the string is not NULL.
        if (s != NULL)
        
        {   
            // Iterate over the characters one at a time.
            for (int i = 0, n = strlen(s); i < n; i++)
            {
                // Checks to see if s[i] is a space character.
                if (s[i] == " ")
                    
                    // Prints the first letter, and the letters after the space character.
                    printf("%c%c%c", s[0], s[i+1], s[i+1]);
     
            }
            
            
              
        }
        
        printf("\n");
    
    
        return 0;
    }
    I'm getting an error when I try to run it though.

    clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow initials.c -lcrypt -lcs50 -lm -o initials
    initials.c:22:22: error: result of comparison against a string literal is unspecified (use strncmp instead) [-Werror,-Wstring-compare]
    if (s[i] == " ")
    ^ ~~~
    initials.c:22:22: error: comparison between pointer and integer ('int' and 'char *') [-Werror]
    if (s[i] == " ")
    ~~~~ ^ ~~~

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you need to use single quotes for a single character.
    Code:
    if (s[i] == ' ')

    Although if you want to psych out your teacher, you could write
    Code:
    if (s[i] == " "[0])
    If you want a total freak-out, write
    Code:
    if (s[i] == 0[" "])
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Person::name and this->name difference?
    By freiza in forum C++ Programming
    Replies: 7
    Last Post: 05-28-2012, 01:20 PM
  2. Randomizing a person's name
    By Naz in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2010, 10:13 PM
  3. Another new person - incrementing
    By MB1 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2005, 06:54 AM
  4. Who is the coolest person here?
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 11-04-2001, 11:42 AM
  5. What should Person A do?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 11-03-2001, 01:43 PM

Tags for this Thread