If we are given a string for instance abcdef and k=2 , so output should be cdefab , i.e. first two characters are appended at end
Here's my code , please tell where's the mistake :
#include<stdio.h>
int main()
{
int count=0,a=0,length=0;
char name[]="abcdef";
char *ptr,*ptr1;
ptr=name;
ptr1=name;
char *name1=name;
while(*ptr!='\0')
{
length++;
ptr++;
}
printf("enter the position by which you have to rotate ");
scanf("%d",&count);
while(a<length )
{
*ptr++=*ptr1++;
name[a]='\0';
name1++;
a++;
}
name1++;
*ptr='\0';
printf("%s",name1);
return 0;
}
How do you want to handle corner cases? - What behavior is required if k is 0? Rotation by 0 characters is the same as no rotation at all.
- What behavior is required when k is negative? Do you need to rotate right?
- What behavior is required when k is greater than or equal to the length of the string?
- What behavior is required when cases 2 and 3 are both true?
- Your program should not fail if the length of the string is 0.
You should test for any illegal k values and report an error if one is found.
8 4412
You could try: - char name[] ="abcdef";
-
char* array = new char[2 + 1];
-
strncpy(array, name, 2);
-
array[2] = '\0';
-
-
strcpy(name, name + 2);
-
strcpy(name + strlen(name), array);
I'm using a k of 2.
The idea is to copy the part that rotates to the end of the string off into a buffer. The strncpy I use does not append a \0 so I append one explicitly.
Then I need to copy the rest of the string forward to the beginning.
The strcpy the string into itself by copying from name+2 to name. This will always work and the \0 of name stops ther strcpy.
All that remains is to copy the original first part of the string (held in array) to the end of the string.
Actually we don't have to use any library function here like strncpy, can you make changes in my code itself and modify it so as to solve the purpose .
How do you want to handle corner cases? - What behavior is required if k is 0? Rotation by 0 characters is the same as no rotation at all.
- What behavior is required when k is negative? Do you need to rotate right?
- What behavior is required when k is greater than or equal to the length of the string?
- What behavior is required when cases 2 and 3 are both true?
- Your program should not fail if the length of the string is 0.
You should test for any illegal k values and report an error if one is found.
The idea of a library function is to save you the effort of reinventing the wheel. Writing code is a last resort. Whenever possible, use a library function.
Of course, if this class specifically says to not use a library function, then you would write from scratch.
As to debugging your code, have you stepped through it using your debugger?
No , I have no idea regarding which debugger to use . Even I tried with online debugger but it takes too long to respond so that is why I never worked with debuggers.
What compiler are you using? For example, Visual Studio includes a debugger that lets you step through your code and be able to see the contents of your variables.
It's important to learn to debug. More important than coding.
I am using gcc on windows . MINGW 32-gcc for windows
Sign in to post your reply or Sign up for a free account.
Similar topics
by: zjut |
last post by:
I need to implement the method : round(String name, int index)
The given string maybe the every type of float type, ( the msdn given the
regax is that :
integral-digits]exponential-digits])
...
|
by: kd |
last post by:
Hi All,
Is there a command to check whether a given string is present in a text
file, without having to read the contents of the file, a line at a time and
then check for the given string?
...
|
by: varsha.gadekar |
last post by:
what is the easy way to check if the given string is substring of other
string only using c++?
|
by: Kelly B |
last post by:
I tried to write a code which would reverse the order of words in a
given string.
I.e if
Input String=The C Programming Language
Output String=Language Programming C The
Here is the code...
|
by: redgrL86 |
last post by:
Hi,
I am trying to figure out how to see if any of a given set of childnodes equals a given string. For example, in the XML and XSL code below, I want the text in the "xsl:when" statement to output...
|
by: mandyj |
last post by:
Given String:-
ST200STT:35697600114106009,20070407,08:09:00,+18.511246,+73.842756,1.430,332.87,1,0000,0
I wanna separate this string in the format below,pls help me out...
35697600114106009 =...
|
by: vaskarbasak |
last post by:
Hi All,
1.How can i convert a String to Unicode characters?
2.Have there any way to get the Unicode range for a given String?
Thanks!
vaskar
|
by: rajeevs |
last post by:
Hi
Is there a way we can generate random alphabet combination from a given string? I am looking for a 3 letter combination from a given string by using vba.
Before or after generating it should...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |