0

I made this program that takes a name and prints the initials, but...it takes the name in the command line. How can I use get_string() to create an array of strings instead of argv, and argc wich is very unorthodox and sloppy, so I can prompt the user to give me her/his name. Thank you.

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(int argc, string argv[])
{

//How do I use Get_string() so I don't have to use argv and argc?? 


//iterate over strings on the vector (words)
    for (int i = 0; i < argc; i++)
    {
        //prints the 0 character of each string, use "toupper" to convert into capital letters
        printf("%c", toupper(argv[i][0]));

    }
    printf("\n");
}

UPDATE

Hi! I'm still stuck but I think I'm in the right path. I substitute argc for an int that counts the chars, and argv will be the get_int() string, but I'm afraid I am missing something since I don't know how to make an array of words, that stores each of the words of the user input as diferents elements of the array. This is what I got so far, and the error is

error: subscripted value is not an array, pointer, or vector

printf("%c", toupper(name[i][0]));

This is the code that I have:

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
printf("Enter your name and last name:\n");

string name = get_string(); //this will be like argv?
int c = strlen(name); //this will be like argc

//iterate over characters
for (int i = 0; i < c; i++)
{
    //prints the 0 of i, that means the 0 char of each word, use "toupper" to convert into capital letters
     printf("%c", toupper(name[i][0]));

}
printf("\n");
}

1 Answer 1

0

We can store the string in a variable as follows:

printf("Enter your name\n")
string name = get_string()

And then use the array notation as always, so that we can do: name[0], name[1], etc

EDIT: The variable name is already an array of characters, so we can access its characters individually through a for loop for example:

for(int i = 0, n = strlen(name); i < n; i++)
    {
        // to do something
        printf("%c", toupper(name[i]));
    }

Converts all characters to capital letters and prints them

2
  • Thank you MARS! I used it but I don't know how to make an array of strings, I mean an array of words wich each word is a position in the array so I can iterate over them to get the first character of each one. I added my changes above. Thank you :D
    – MariaPilar
    Commented Jun 29, 2017 at 2:53
  • you can read my edit
    – MARS
    Commented Jun 29, 2017 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .