The code is suppose to print the initials of the users but the code has a bug instead it prints out the whole name with spaces between each letter. I know the bug lies in the for loop but im not sure how to debug this issue. Suggestions?
int main(void)
{
printf("Enter full name: ");
string name = get_string();
{
printf("%c", toupper(name[0]));
}
for(int i = 0, n = strlen(name); i < n; i++)
{
printf(" ");
printf("%c", toupper(name[i + 1]));
}
}
I think that you mean to test to see if the next char is a space, and if it is, then print the char following the space:
if (name[i] == ' ')
{
printf("%c", toupper(name[i + 1]));
}
Of course, before the for loop, you need to print the first initial since there won't be a space before it:
// print the first initial before the loop
cout << static_cast<unsigned char>(toupper(name[0]));
You are correctly printing the characters, but you have printf(" "); so no wonder why there are spaces here.
Related
Write a C program to split an input string (variable “name”) into two output strings (variables “first”
and “last”). Assume that the user provides input containing only the characters ‘a’ through ‘z’ and ‘A’
through ‘Z’. Assume there are exactly two capital letters in the input, one at the beginning of the first
name, and one at the beginning of the last name. For example, given the input “JoeSmith”, your code
should split it into “Joe” and “Smith”. If the given input contains only the first name example “Joe”
then it should print ‘only the first name is provided’ and print that first name.
int main() {
char name[20], first[20], last[20];
printf("Enter FirstLast name or enter First: ");
scanf("%s", name);
int i, j, index;
for (i = 0; i <= 20; i++) {
if (i == 0) {
first[i] = name[i];
} else {
if (name[i] >= 'A' && name[i] <= 'Z') {
index = i;
break;
}
first[i] = name[i];
}
}
for (j = index, i = 0; i <= 20 - index; j++, i++) {
last[i] = name[j];
}
first[i + 1] = '\0';
last[j + 1] = '\0';
if (first[i]) printf("Only first name printed%s\n", first);
printf("first name:%s\nlast name:%s", first, last);
return 0;
}
how would I get the code to print out "only first name printed" and print the first name beneath it? having trouble figuring this out. Also keeping the FirstLast name aswell.
When I input BobJoe
it prints
First name:Bob
Last name:Joe
Which is one of the two things I need^^
When I input
Bob
it prints
zsh: segmentation fault ./run
Trying out your code with regard to the comments made above, I offer up a revised version of your code utilizing the "string.h" include file to provide a bit better control.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[20], first[20], last[20];
int i = 0, j = 0, caps = 0, index;
printf("Enter FirstLast name or enter First: ");
scanf("%s", name);
for (index = 0; index < 20; index++) /* Initialize the first and last name arrays to avoid unknown character data */
{
first[index] = '\0';
last[index] = '\0';
}
for (index = 0; index < strlen(name); index++)
{
if (name[index] >= 'A' && name[index] <= 'Z')
{
caps++;
}
if (caps <= 1)
{
first[i] = name[index];
i++;
}
else
{
last[j] = name[index];
j++;
}
}
if (last[0] == '\0')
printf("Only first name printed: %s\n", first);
else
printf("first name: %s\nlast name: %s\n", first, last);
return 0;
}
To ensure that the resulting first and last name arrays do not have uninitialized data, these are initialized prior to usage. Then, in the program the "strlen" function is utilized to ensure that testing and copying does not run past the end of your input character array.
The result in testing these changes are as follows in the terminal output.
#Una:~/C_Programs/Console/FirstLast/bin/Release$ ./FirstLast
Enter FirstLast name or enter First: JoeSmith
first name: Joe
last name: Smith
#Una:~/C_Programs/Console/FirstLast/bin/Release$ ./FirstLast
Enter FirstLast name or enter First: Joe
Only first name printed: Joe
#Una:~/C_Programs/Console/FirstLast/bin/Release$ ./FirstLast
Enter FirstLast name or enter First: joeSmith
Only first name printed: joeSmith
Give that a try to see if this meets the spirit of your project.
I am trying to print characters of a given string at even and odd positions of using c program. My program is working fine if there is only one string but it not working for a sentence containing more than one string.The following code only prints the second string entered, but it does not printing the characters at even and odd positions as it was doing for the very first string.
int main()
{
char string[10], even[10], odd[10], i, j, k,count;
i = j = k = count = 0;
do
{
printf("Enter your input string:");
fgets(string, 10, stdin);
string[strlen(string) - 1] = '\0';
/* printing the input string */
printf("Given Input string:%s\n", string);
while (string[i] != '\0')
{
if (i % 2 == 0) {
odd[j++] = string[i];
}
else {
even[k++] = string[i];
}
i++;
}
/* terminating even and odd string with NULL */
odd[j] = even[k] = '\0';
/* print the characters at odd position and even positions */
printf(" %s %s", odd,even);
odd[0]=even[0]='\0';
count++;
}while(count<2);
return 0;
}
At execution when I entered the first string as "united", it printed out "uie" for the odd position and "ntd" for the even position. Then I am prompted to enter the second string which I entered as "states" but nothing printed out except the string entered and the program exits. It did not output as it did for the first string.
Please help me to point out my mistake so that my code should work correctly for any number of strings rather than only for first string i.e it should output the characters at even and odd positions for all of the strings entered by the user.
You are restricting yourself to an archaic version of C syntax that required all local variables to be declared at the top of a block, ahead of any executable statements.
Since your code is not using variables other than count outside the do/while loop, you should move their declarations inside the loop. This will ensure that the variables have appropriate initial values at the beginning of each iteration.
Here are a few additional points to keep in mind:
Your code will result in undefined behavior if an end-user terminates the input stream (Ctrl+Z on Windows, Ctrl+D on UNIX) without entering any characters
Your code will drop the last character when end-user terminates input stream after entering less than ten characters
odd[0]=even[0]='\0' is unnecessary
using namespace std;
struct str
{
char s[10000];
};
int main() {
int T;
cin >> T;
fflush(stdin);
str s1[10];
while (T--) {
cin >> s1[T].s;
fflush(stdin);
int j = 0;
while (j < strlen(s1[T].s)) {
if (j % 2 == 0)
cout << s1[T].s[j];
++j;
}
cout << " ";
int k = 0;
while (k < strlen(s1[T].s)) {
if (k % 2 == 1)
cout << s1[T].s[k];
++k;
}
cout << endl;
}
return 0;
}
I need to write a simple program (no fancy pointer stuff, no library functions. It's for educational purposes) that reads the first and second names from a user and prints them out in a single line separated by a space. I am not getting the result and I am not sure why:
# include <stdio.h>
//the program loosely simulates the behaviour of strcpy
main(){
char fname[16], sname[16], cat[31];
int i, j;
printf("Please enter your first name: ");
scanf("%s", fname);
printf("Please enter your second name: ");
scanf("%s", sname);
for (i=0; fname[i] != '\0'; cat[i++] = fname[i++])
;
cat[i+1] = ' '; //adds a space between the tokens
for (j=i+1; sname[j] != '\0'; cat[j++] = sname[j++])
;
printf("The final result is:\n%s", cat);
return 0;
}
You have several problems. First, since cat has to be big enough to hold the first two strings and a space between them, it should be declared cat[32] -- 15 characters of first name, 15 characters of surname, 1 space, and 1 trailing null byte.
You're putting the space between the words in the wrong place. The first loop left i holding the next position in cat, so it should be:
cat[i] = ' ';
Next, your array indexes in the second loop are incorrect. The positions in cat are correct, because they start from where you left off the previous loop. But you need to start from 0 in sname. So this loop should be:
for (j = i+1, k = 0; sname[k] != 0; cat[j++] = sname[k++])
;
Finally, after concatenating the two strings, you need to append a null byte to the result, to indicate the end.
cat[j] = 0;
Another problem is that you're incrementing i twice each time through the first loop, since you use cat[i++] = fname[i++]. Each of those i++ will increment the variable. You need to separate the assignment from the increments:
for (i=0; fname[i] != '\0'; i++) {
cat[i] = fname[i];
}
Here's a final version of the script that works:
# include <stdio.h>
//the program loosely simulates the behaviour of strcpy
int main() {
char fname[16], sname[16], cat[32];
int i, j, k;
printf("Please enter your first name: ");
scanf("%s", fname);
printf("Please enter your second name: ");
scanf("%s", sname);
for (i=0; fname[i] != '\0'; i++) {
cat[i] = fname[i];
}
cat[i] = ' ';
for (j = i+1, k = 0; sname[k] != 0; cat[j++] = sname[k++]) {
}
cat[j] = 0;
printf("The final result is: %s\n", cat);
return 0;
}
I realize you set yourself a challenge to try to learn how to do something specific (and I see you are making progress towards your goal). But I always like to keep in mind there are lots of ways to get the job done -- especially in C. You know you could just do this with printf, right?
char fname[16], sname[16];
int i, j;
printf("Please enter your first name: ");
scanf("%s", fname);
printf("Please enter your second name: ");
scanf("%s", sname);
printf("%s %s\n", fname, sname);
Watch the index you use :
cat[i++] = fname[i++] and cat[j++] = sname[j++]
try to increment 'i' and 'j' at the end of your loop :
for (i=0; fname[i] != '\0'; ++i)
cat[i] = fname[i];
// ...
for (j=0; sname[j] != '\0';++j)
cat[j+i+1] = sname[j];
I need to write a program that will read in a sentence and output the number of words in the sentence. I have the program done, but the problem is that my program is counting the spaces inbetween the words as characters. How do I omit those spaces and just display the number of words in the string? I was thinking I need some type of loop, but I don't know how to execute it.
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define pause system("pause")
main() {
char mystring[155];
int counter = 0;
printf("Enter your name: ");
scanf("%[^\t\n]", &mystring);
printf("Your name is %s\n", mystring);
// find out the number of characters in the string
counter = strlen(mystring);
printf("There are %i words in the sentence. \n", counter);
// find out how many WORDS are in the sentence. Omit spaces
pause;
} // end of main
Again, as someone has already said, use strtok. If you need to know more about it,
http://www.cplusplus.com/reference/cstring/strtok/
If you want to do it without using any existing API's (I don't know why you would want to do that unless it's a class project), then create a simple algorithm with a pointer to traverse the string and skip spaces while incrementing count.
As people have said before, this will be a good exercise for you, so don't ask for code. And there's always google..
Your function would look something like this :
_getNumberOfWords(char[] string) {
int count = 0;
for (int i=0; string[i] != '\0'; i++) {
if (string[i] == " ") {
for (int j=i; string[j] != '\0'; j++) {
// This is to handle multiple spaces
if (string[j] != " ") break;
}
count++;
}
return count;
}
You could also try to do a :
char * strtok ( char * string, const char * " " );
"Have a program request the user to enter an uppercase letter. Use nested loops to produce a pyramid pattern like this:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
The pattern should extend to the character entered. For example, the preceding pattern would result from an input value of E."
So far I've been doing this for a good few hours and I'm getting the 'pyramid' to format properly for the letters when iterating forwards through the alphabet with:
#include <stdio.h>
int main(void)
{
char ch = 0;
char ch2 = 0;
int rows = 0;
printf("Enter a character: ");
scanf("%c", &ch);
rows = ch - 64;
while(rows > 0)
{
int spaces;
for(spaces = rows-1; spaces > 0; spaces--)
{
printf(" ");
}
ch2 = 65;
while(ch2 < (ch-(rows-2)))
{
printf("%c", ch2);
ch2++;
}
printf("\n");
rows--;
}
}
However, I feel as though I've hit a brick wall with trying to get it to iterate backwards properly. I know it should only be a few basic loops but I'm well and truly stuck. I'm sure it's easy... I think I've just been looking at it too long. Ideas?
You are so close, you only need to take a breath and you'll see it.
When you print out your character, it has to be done after this part
while(ch2 < (ch-(rows-2)))
{
printf("%c", ch2);
ch2++;
}
or it won't fall at the end of the string. What you need is another loop that starts at the character that's one below the last character printed. It should print a character and decrement that character until it has printed the 'A' character.
Since this is homework, I'll give you a chance to write that loop before telling you the exact details.
There are ways this code could probably be rewritten to make it clearer, but basing on what you have, something like this would probably work right after your current while loop.
while (ch2 > 'A')
{
ch2--;
printf("%c", ch2);
}
I do recommend attempting to refactor your code a bit to make it clearer, though. As I suggested in a comment, start off by using character literals rather than raw integers.
You can iterate down as well as up:
while(ch2 >= 'A')
{
printf("%c", ch2);
ch2--;
}
Try this:
#include <stdio.h>
int main (int argc, const char * argv[])
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if(ch<'A' || ch>'Z'){
printf("Character must be between 'A' and 'Z'\n");
return 1;
}
for(int rows = ch - 'A'; rows >= 0; rows--)
{
char ch2;
for(int spaces = rows; spaces > 0; spaces--)
printf(" ");
for(ch2='A'; ch2 < (ch-(rows-1)); ch2++)
printf("%c", ch2);
for(ch2=ch2-2;ch2>='A';ch2--)
printf("%c", ch2);
printf("\n");
}
return 0;
}