473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to left rotate a given string by k positions ?

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;
}
Apr 8 '16 #1

✓ answered by donbock

How do you want to handle corner cases?
  1. What behavior is required if k is 0? Rotation by 0 characters is the same as no rotation at all.
  2. What behavior is required when k is negative? Do you need to rotate right?
  3. What behavior is required when k is greater than or equal to the length of the string?
  4. What behavior is required when cases 2 and 3 are both true?
  5. 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
weaknessforcats
9,208 Expert Mod 8TB
You could try:
Expand|Select|Wrap|Line Numbers
  1. char name[] ="abcdef";
  2.         char* array = new char[2 + 1];
  3.     strncpy(array, name, 2);
  4.     array[2] = '\0';
  5.  
  6.     strcpy(name, name + 2);
  7.     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.
Apr 8 '16 #2
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 .
Apr 9 '16 #3
donbock
2,426 Expert 2GB
How do you want to handle corner cases?
  1. What behavior is required if k is 0? Rotation by 0 characters is the same as no rotation at all.
  2. What behavior is required when k is negative? Do you need to rotate right?
  3. What behavior is required when k is greater than or equal to the length of the string?
  4. What behavior is required when cases 2 and 3 are both true?
  5. 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.
Apr 9 '16 #4
weaknessforcats
9,208 Expert Mod 8TB
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?
Apr 9 '16 #5
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.
Apr 9 '16 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 9 '16 #7
I am using gcc on windows . MINGW 32-gcc for windows
Apr 10 '16 #8
weaknessforcats
9,208 Expert Mod 8TB
Here you go: http://www.tutorialspoint.com/gnu_debugger/.

Let me know how it goes.
Apr 10 '16 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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]) ...
10
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? ...
8
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++?
2
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...
1
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...
10
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 =...
1
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
8
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.