Working on a problem and cannot figure out this part.....
Printable View
Working on a problem and cannot figure out this part.....
Post your code up to what you tried so far.
Code:#include <stdio.h>
#include <string.h>
#define STR_LEN 30
#define N 10
int main(void)
{
int a[30] [20];
char str[STR_LEN + 1];
printf("Enter a sentence in all lowercase letters ending with a ''.'', ''?'', or ''!'' : \n" );
for(i = 0; i < N; i++)
scanf("%s", str);
printf("Reversal of sentence:" );
for(i = N - 1; i >= 0; i--)
printf("%d", a[i]);
printf("\n");
return 0;
}
Actually what is your requirement . Whether you want to store each word in a row or reverse a string .
Right now i'm working on storing each word in a row; the reverse part i haven't got to yet. Their are like 4 different things that are wanted out of this question, but right now the first step is for a person to enter a sentence ending with a . ? or ! and then I have to store what they enter as a row of char array. I have been on this for about 4 dang hours,
ok.I think my code will help you.
I used a newline as a sentence terminator.You can change it to '.' or '?' or '!'.
Code:
#include<stdio.h>
#include<string.h>
main()
{
char grades[30][20];
int i=0,j=0,k=0;
char c;
printf("Enter the sentence : ");
while((c=getchar()))
{
if(c==' ' || c=='\n' || c=='\t')
{
grades[i][j]='\0'; // To add a null the make it as a separate string
i++;
j=0;
}
else
{
grades[i][j]=c; assign the character in the array
j++;
}
if(c=='\n')
break; terminate if the sentence get over.
}
for(j=0;j<i;j++)
printf("%s\n",grades[j]);
}
Thanks
ok, thank you so much, can you break this code down to exactly where I am looking to take what they enter and store it into a char array? (sorry, very new c programmer)
I think this is your home work.
no, actually I am working problems from a worksheet to prepare me for a quiz
I think the first big concept in dealing with strings or char's is this:
Now is the time for all good men to come to the aid of their country.
That may look like a sentence, and therefore a string, but NOT in C!!
In C, a bunch of chars, are just a bunch of chars, *unless* they have an end of string char immediately after them, to mark it as a string.
This is the end of string char: '\0', and this is a string in C:
Now is the time for all good men to come to the aid of their country.'\0'
Whether the char's include a newline: '\n' char or not, doesn't matter. That just changes how the chars will be printed or displayed.
Usually, you want to deal with chars, as a single string. Not always, but 95% of the time, you do. And it has to have that end of string char, or all your string functions you want to use, like printing for instance:
printf("%s", mystringArrayName);
will not work right.
thank you for explaining that adak
Let's look at Karthigayan's program snippet:
Nice clear and simple logic, and nice indentation - which is important in C, because the code becomes so much easier to read. Especially after you've done it for awhile, your eye just gets trained to catch the errors with standard indentation of subordinate lines of code.Code:
#include<stdio.h> //you'll almost always want this for basic i/o functions, etc.
#include<string.h> //for working with the string functions.
int main() //in C this should always be int main() for compatibility reasons
{
char grades[30][20]; //our char array: 30 rows, 20 columns across
int i=0,j=0,k=0; //a few int's, all assigned zero value
char c; //a single char
Question1: What can a single char NEVER become in C?
printf("Enter the sentence : ");
while((c=getchar())) //get one char while they're in the keyboard buffer
{
//if the char is a space, newline or tab
if(c==' ' || c=='\n' || c=='\t')
{
//add the end of string char to the array
grades[i][j]='\0'; // To add a null the make it as a separate string
i++; //increment the row number
j=0; //set the column number to zero.
}
else //it wasn't a space, newline or tab char
{
grades[i][j]=c; //assign the character in the array
j++; //increment the column number, ready for the next char
}
if(c=='\n')
break; //terminate if the sentence is finished.
}
for(j=0;j<i;j++) //for each row
printf("%s\n",grades[j]); //print the string we made
/* this is the int being returned to the operating system. It can be used to find out
if the program finished right or not.
*/
return 0;
}
Question 2: What punctuation mark is not included in this program, that ends most sentences?
Question 3: This is used in the program: == What's the difference between = and == and which one do you always want to use inside an if statement?
================================================== ===
1) A string. It doesn't have enough space to hold a char, and the end of string char.
2) A period.
3) One = is assignment: x = 3; Two == is a comparison: if(x == 2). Two =='s are always used in an if statement.
Hey Adak, I have a question about what you said here. When am I supposed to use '\0'?? Ive been fscanf strings for the past 2-3 programs and I've never heard of this, o_O. so have i been doing it wrong? I have always come up with many problems getting the strings or so but ive always fixed them never seeing the '\0' rule.
Is that why I have my current problem for string here?
http://cboard.cprogramming.com/c-pro...-confused.html
the name part, instead of printing jas its printing H*n. is it because I don't have some '\0' somehow?