I am having trouble finishing my program. I am supposed to make one that prints the users name and then procedes to print out the initials. Here is my code right now:
#include <stdio.h>
#include "csc111.h"
#include <string.h>
#include <ctype.h>
int main (void) {
//Declare variables
char name[50];
int i;
string myString;
string nameLength;
//Prompt user for namespace
printf("Please enter your name: ");
scanf("%s", myString);
//Load an array with letters 'a' through 'z'
for(i=0;i<2;i++) {
name[i] = i +97;
}
name[10] = '\0';
//Convert the string to all uppercase letters
for(i=0;i<strlen(myString);i++) {
myString[i] = toupper(myString[i]);
}
while (name[i]!='\0')
{
if (name[i]==' ')
{
i++;
printf("%s", name[i+1]);
}
i++;
}
printf("%c", name[i+1]);
//Print initials
printf("Your initials are: %s\n", myString);
}
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
//Load an array with letters 'a' through 'z'
for(i=0;i<2;i++) {
name[i] = i +97;
}
name[10] = '\0';
//Convert the string to all uppercase letters
for(i=0;i<strlen(myString);i++) {
myString[i] = toupper(myString[i]);
}
Howdy - Would you mind explaining what you are trying to do with lines 20-28? I don’t think that they help you towards printing a user name and then initials.
while (name[i]!='\0')
{
if (name[i]==' ')
{
i++;
printf("%s", name[i+1]);
}
i++;
}
I think that lines 29-37 have some of the logic you need to print the initials, but I think that you are missing the first initial. You are incrementing your iterator before printing the next character when you find a space, which will cause you troubles.
Hi! Thank you so much for replying to my post! I guess I tried to do something from my book in lines 20-25, but on the next few lines of code I was trying to make it print the initials capitalized. I’m really sorry if my code is messy, I am a young girl and all new to this
I think that I would try to get the initials printing first and then focus on capitalization. If you can find and print the initials, then you can change your code to find, capitalize, and then print the initials.