Can somebody please give me a hand on this.I don't know how to create this programme.
Make a program that will turn backwards the spelling of the word being input (in roman letters).
Example: EATTAE
hint: put the suffix and read each letter as one unit
eg: copy the first letter to the 5th letter
x[0]= x[4];
*when u make the circular letters , don't forget to set the end part of the word
13 replies to this topic
#1
Posted 12 November 2011 - 07:06 PM
|
|
|
#2
Posted 13 November 2011 - 03:37 AM
Have you looked at : Strings library - C++ Reference
My idea is to check string length(there is a function for that in string library), then use swap(also in string) on first and last char. then swap second and last -1 ... and so on(for loop will do it) until you have one or 0 characters left. Dont forget to subtract 1 from string length cause you are starting from 0 not from 1.
In my opinion this way is much more easier than using copy.
My idea is to check string length(there is a function for that in string library), then use swap(also in string) on first and last char. then swap second and last -1 ... and so on(for loop will do it) until you have one or 0 characters left. Dont forget to subtract 1 from string length cause you are starting from 0 not from 1.
In my opinion this way is much more easier than using copy.
#3
Posted 13 November 2011 - 07:17 AM
I think the key point here is to understand you can refer to a character in any string as if you are referring an element in an array. For example;
string myString = "pencil";
in this case; myString[0]=p , myString[1]=e... etc;
then all you have to do is using this syntax to reverse a string.
A way of doing this could be like this:
string a; // We want this to be reversed.
string b=""; // This will be the solution. Initially it contains no chars.
int i=0;
int k=a.length()-1; // -1 is because first element's index is 0, instead of 1.
while(k>=0)
{
b[i]=a[k];
k--;
i++;
}
string myString = "pencil";
in this case; myString[0]=p , myString[1]=e... etc;
then all you have to do is using this syntax to reverse a string.
A way of doing this could be like this:
string a; // We want this to be reversed.
string b=""; // This will be the solution. Initially it contains no chars.
int i=0;
int k=a.length()-1; // -1 is because first element's index is 0, instead of 1.
while(k>=0)
{
b[i]=a[k];
k--;
i++;
}
#4
Posted 13 November 2011 - 04:08 PM
I think, performance wise, it's better to use (reverse) iterators as arguments to 2nd string's constructor.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#5
Posted 13 November 2011 - 07:06 PM
This is what I have tried to do by using the reverse function but when I enter a string in the program,it stops working and freezes.What is wrong?
#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
strcat(str,rev);
printf("\n%s",str);
return 0;
}
#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
strcat(str,rev);
printf("\n%s",str);
return 0;
}
#6
Posted 13 November 2011 - 07:46 PM
You just declared pointers to char datatype, you didn't actually reserve space to store data.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#7
Posted 14 November 2011 - 04:42 AM
If you are implementing string.h library why you are using 'char' data type ? Its possible to do it that way, but why invent a wheel again ? You've got everything needed included and you are not using it.
#8
Posted 14 November 2011 - 05:08 AM
Can you explain to me more clearly please?
#9
Posted 14 November 2011 - 08:12 AM
First question: is this C or C++? Appropriate answers will be completely different.
#10
Posted 14 November 2011 - 08:14 AM
This is c++ but I have just solved it. :)
However, can you explain this part here,
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
However, can you explain this part here,
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
#11
Posted 14 November 2011 - 08:19 AM
The comma operator lets you do two things as a "statement", so you increment j AND decrement i on every loop.
#12
Posted 14 November 2011 - 09:02 AM
WingedPanther said:
The comma operator lets you do two things as a "statement", so you increment j AND decrement i on every loop.
What about this one?What does this mean and what is the purpose?
rev[j]='\0';
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









